|
29 | 29 | * Test stub.
|
30 | 30 | */
|
31 | 31 | class Article extends Entity {
|
| 32 | + |
| 33 | +/** |
| 34 | + * Testing stub method. |
| 35 | + * |
| 36 | + * @return bool |
| 37 | + */ |
| 38 | + public function isRequired() { |
| 39 | + return true; |
| 40 | + } |
32 | 41 | }
|
33 | 42 |
|
34 | 43 | /**
|
@@ -647,6 +656,37 @@ public function testIsRequiredAssociatedHasMany() {
|
647 | 656 | $this->assertFalse($context->isRequired(''));
|
648 | 657 | }
|
649 | 658 |
|
| 659 | +/** |
| 660 | + * Test isRequired on associated entities with custom validators. |
| 661 | + * |
| 662 | + * Ensures that missing associations use the correct entity class |
| 663 | + * so provider methods work correctly. |
| 664 | + * |
| 665 | + * @return void |
| 666 | + */ |
| 667 | + public function testIsRequiredAssociatedCustomValidator() { |
| 668 | + $this->_setupTables(); |
| 669 | + $users = TableRegistry::get('Users'); |
| 670 | + $articles = TableRegistry::get('Articles'); |
| 671 | + |
| 672 | + $validator = $articles->validator(); |
| 673 | + $validator->notEmpty('title', 'nope', function ($context) { |
| 674 | + return $context['providers']['entity']->isRequired(); |
| 675 | + }); |
| 676 | + $articles->validator('default', $validator); |
| 677 | + |
| 678 | + $row = new Entity([ |
| 679 | + 'username' => 'mark' |
| 680 | + ]); |
| 681 | + $context = new EntityContext($this->request, [ |
| 682 | + 'entity' => $row, |
| 683 | + 'table' => 'Users', |
| 684 | + 'validator' => 'default', |
| 685 | + ]); |
| 686 | + |
| 687 | + $this->assertTrue($context->isRequired('articles.0.title')); |
| 688 | + } |
| 689 | + |
650 | 690 | /**
|
651 | 691 | * Test isRequired on associated entities.
|
652 | 692 | *
|
@@ -943,6 +983,7 @@ protected function _setupTables() {
|
943 | 983 | $articles = TableRegistry::get('Articles');
|
944 | 984 | $articles->belongsTo('Users');
|
945 | 985 | $articles->hasMany('Comments');
|
| 986 | + $articles->entityClass(__NAMESPACE__ . '\Article'); |
946 | 987 |
|
947 | 988 | $comments = TableRegistry::get('Comments');
|
948 | 989 | $users = TableRegistry::get('Users');
|
|
0 commit comments