Log message #4208887

# At Username Text
# Oct 15th 2019, 07:53 spriz I see :thinking_face: But those errors in the `buildRules` is also shown in the UI - aye?
# Oct 15th 2019, 07:47 neon1024 Morning everyone :wave:
# Oct 15th 2019, 07:47 conehead :P
# Oct 15th 2019, 07:41 ruth.cantoni facade
# Oct 15th 2019, 07:38 dereuromark Pretty sure i once Blogged about this^^
# Oct 15th 2019, 07:37 dereuromark Actually that is how it should be done correctly for usability.
# Oct 15th 2019, 07:37 dereuromark @conehead it might just be the status quo of passing tests :)
# Oct 15th 2019, 07:19 spriz That looks like sorcery usage of `Validator` :O :)
# Oct 15th 2019, 06:22 conehead @tippexs91 I don't get your test case? You expect that the result is empty twice. So actually it never should be saved?
# Oct 14th 2019, 22:00 dereuromark https://github.com/dereuromark/cakephp-sandbox/blob/master/plugins/Sandbox/src/Model/Table/AnimalsTable.php#L49 works perfectly for me
# Oct 14th 2019, 21:58 dereuromark so how did you set up the validation? because usually it works.
# Oct 14th 2019, 21:53 tippexs91 make sense to me, but still the question. How to write a suitable test for isUnique Validations? Would be very cool to have the possibility to test this before releasing the production build.
# Oct 14th 2019, 21:49 challgren This would be a prime example of returning a http 402
# Oct 14th 2019, 21:48 tippexs91 Yeah. In this case we need to deal with a not very stable sub-system sending updates to our payments. Therefore the payment_id need to be unique. If I have a entry for a given payment_id already, i will not save the current one and send a 400.
# Oct 14th 2019, 21:37 dereuromark rules are best for data integrity check, especially if you want to catch db constraints early. but for me isUnique is part of validation and should find its way into meaningful user output.
# Oct 14th 2019, 21:33 tippexs91 okay to be clear... validations are good for sementical checks... but not for checks like that?
# Oct 14th 2019, 21:31 dereuromark i sure dont do that :slightly_smiling_face:
# Oct 14th 2019, 21:31 tippexs91 @dereuromark means i should not use this in my table obj ``` public function buildRules(RulesChecker $rules) { $rules->add($rules->existsIn(['payment_id'], 'Payments')); $rules->add($rules->isUnique(['payment_id'])); return $rules; } ```
# Oct 14th 2019, 21:27 dereuromark it is also always better/cleaner to use validation here, as that would be also properly displayed with other validation issues.
# Oct 14th 2019, 21:27 tippexs91 ` // A single field. $rules->add($rules->isUnique(['email'])); `
# Oct 14th 2019, 21:27 dereuromark yeah you are looking for validation rule validateUnique :slightly_smiling_face:
# Oct 14th 2019, 21:27 daniel.upshaw Your `isUnique` is in there, that's probably right... but would think that goes in validation
# Oct 14th 2019, 21:27 daniel.upshaw Oh hmm
# Oct 14th 2019, 21:26 daniel.upshaw Looks like `buildRules` is for associations?
# Oct 14th 2019, 21:26 daniel.upshaw Hmm
# Oct 14th 2019, 21:26 daniel.upshaw ``` * Default validation rules. * * @param \Cake\Validation\Validator $validator Validator instance. * @return \Cake\Validation\Validator */ public function validationDefault(Validator $validator)```
# Oct 14th 2019, 21:26 daniel.upshaw ``` * Returns a rules checker object that will be used for validating * application integrity. * * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. * @return \Cake\ORM\RulesChecker */ public function buildRules(RulesChecker $rules)```
# Oct 14th 2019, 21:25 tippexs91 Oh... Maybe testValidationDefault? Should I add something here?
# Oct 14th 2019, 21:23 tippexs91 Still passing. https://gist.github.com/tippexs/87f3b127f3022263cc1679325e111c38
# Oct 14th 2019, 21:22 dereuromark assertFalse is cleaner usually if it returns entity or false.
# Oct 14th 2019, 21:20 slackebot1 $this->assertEmpty($this->Invoices->save($entity)); } ```
# Oct 14th 2019, 21:20 tippexs91 ``` public function testIsUniquePaymentId() { $config = TableRegistry::exists('Invoices') ? [] : ['className' => InvoicesTable::class]; $this->Invoices = TableRegistry::get('Invoices', $config); $entity = $this->Invoices->newEntity(['payment_id' => 1]); $this->assertEmpty($this->Invoices->save($entity)); $entity = $this->Invoices->newEntity(['payment_id' => 1]);
# Oct 14th 2019, 21:17 tippexs91 will give it a try
# Oct 14th 2019, 21:17 tippexs91 Hi @dereuromark. Perfect. Sounds great. So create a new Entity with `$this->Invoices->newEntity([])` and try `$this-Invoices->save($entity)`
# Oct 14th 2019, 21:12 dereuromark Yeah, you can test e.g. save() directly in Table tests
# Oct 14th 2019, 21:04 slackebot1 $entity = $invoices->newEntity(['invoicenr' => 'TestInvoice01', 'payment_id' => 1]); $this->assertEmpty($entity->getErrors()); } ``` How to test the isUnique Validation? For me, it feels lightly wrong to test it in the controller? Is it possible to test the validations in the TableTests? Thanks for your help
# Oct 14th 2019, 21:04 tippexs91 hi @here. Any Testing-Specialist arround? I need to test the Validation-Rules. ``` // InvoicesTable public function buildRules(RulesChecker $rules) { $rules->add($rules->existsIn(['payment_id'], 'Payments')); $rules->add($rules->isUnique(['payment_id'])); return $rules; ``` The Controller-Test ``` public function testAdd() { $invoices = TableRegistry::getTableLocator()->get('Invoices');
# Oct 14th 2019, 19:08 daniel.upshaw It's a `select` element on the page
# Oct 14th 2019, 19:01 daniel.upshaw If I add `$this->Form->unlockField('new_field_name');`, it works, but I can't see why it wouldn't treat it like every other field in the table/model
# Oct 14th 2019, 18:59 daniel.upshaw This particular application uses `"cakephp/cakephp": "3.5.*"`, so hopefully that's not why
# Oct 14th 2019, 18:58 daniel.upshaw This is empty, but if I remove the input field that supplies the post data for that column, the form still saves