Log message #4208879

# At Username Text
# 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
# Oct 14th 2019, 18:57 daniel.upshaw `<input type="hidden" name="_Token[unlocked]" autocomplete="off" value="">`
# Oct 14th 2019, 18:57 daniel.upshaw `Unexpected field in POST data` after adding a new column :S It's added to the `Entity` as accessible, as well as the validator in the `Table`.. The form is created the standard way of `$this->Form->create($modelName);`
# Oct 14th 2019, 18:34 ndm The plugin installer updates that file when the composer autoloader is dumped. https://github.com/cakephp/app/blob/master/composer.json#L40 It handles both types of plugins, local ones, and ones that are composer dependencies. Bake will trigger the `dump-autoload` command when a plugin is baked.
# Oct 14th 2019, 18:27 dereuromark Then you need to declare it in root composer.json. see my sandbox for examples.
# Oct 14th 2019, 18:25 david mmm, but my plugin is not loaded via composer
# Oct 14th 2019, 18:24 dereuromark composer plugin itself
# Oct 14th 2019, 18:23 david which command has updated my vendor/cakephp-plugins.php ?
# Oct 14th 2019, 18:20 graziel no problem its just probably something silly you will find in time