| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\ORM;
- use Cake\ORM\Entity;
- use Cake\ORM\EntityValidator;
- use Cake\ORM\Table;
- use Cake\ORM\TableRegistry;
- use Cake\TestSuite\TestCase;
- /**
- * EntityValidator test
- */
- class EntityValidatorTest extends TestCase
- {
- /**
- * setup
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $articles = TableRegistry::get('Articles');
- $users = TableRegistry::get('Users');
- $articles->belongsTo('Users');
- $articles->hasMany('Comments');
- $comments = TableRegistry::get('Comments');
- $comments->belongsTo('Articles');
- $comments->belongsTo('Users');
- $this->articles = $articles;
- $this->comments = $comments;
- $this->users = $users;
- }
- /**
- * Teardown
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- TableRegistry::clear();
- unset($this->articles, $this->comments, $this->users);
- }
- /**
- * Test one() with successful validate
- *
- * @return void
- */
- public function testOneSuccess()
- {
- $entity = $this->getMock('TestApp\Model\Entity\ValidatableEntity', ['validate']);
- $validator = $this->getMock('\Cake\Validation\Validator');
- $this->articles->validator('default', $validator);
- $entityValidator = new EntityValidator($this->articles);
- $validator->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- $entity->expects($this->once())
- ->method('validate')
- ->with($validator)
- ->will($this->returnValue([]));
- $this->assertTrue($entityValidator->one($entity));
- }
- /**
- * Test one() with failing validate
- *
- * @return void
- */
- public function testOneFail()
- {
- $entity = $this->getMock('TestApp\Model\Entity\ValidatableEntity', ['validate']);
- $validator = $this->getMock('\Cake\Validation\Validator');
- $this->articles->validator('default', $validator);
- $entityValidator = new EntityValidator($this->articles);
- $validator->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- $entity->expects($this->once())
- ->method('validate')
- ->with($validator)
- ->will($this->returnValue(['one' => ['error']]));
- $this->assertFalse($entityValidator->one($entity));
- }
- /**
- * test one() with association data.
- *
- * @return void
- */
- public function testOneAssociationsSuccess()
- {
- $class = 'TestApp\Model\Entity\ValidatableEntity';
- $article = $this->getMock($class, ['validate']);
- $comment1 = $this->getMock($class, ['validate']);
- $comment2 = $this->getMock($class, ['validate']);
- $user = $this->getMock($class, ['validate']);
- $article->set('comments', [$comment1, $comment2]);
- $article->set('user', $user);
- $validator1 = $this->getMock('\Cake\Validation\Validator');
- $validator2 = $this->getMock('\Cake\Validation\Validator');
- $validator3 = $this->getMock('\Cake\Validation\Validator');
- $validator1->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- $validator2->expects($this->exactly(2))
- ->method('count')
- ->will($this->returnValue(1));
- $validator3->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- $this->articles->validator('default', $validator1);
- $this->comments->validator('default', $validator2);
- $this->users->validator('default', $validator3);
- $entityValidator = new EntityValidator($this->articles);
- $article->expects($this->once())
- ->method('validate')
- ->with($validator1)
- ->will($this->returnValue([]));
- $comment1->expects($this->once())
- ->method('validate')
- ->with($validator2)
- ->will($this->returnValue([]));
- $comment2->expects($this->once())
- ->method('validate')
- ->with($validator2)
- ->will($this->returnValue([]));
- $user->expects($this->once())
- ->method('validate')
- ->with($validator3)
- ->will($this->returnValue([]));
- $options = ['associated' => ['Comments', 'Users']];
- $this->assertTrue($entityValidator->one($article, $options));
- }
- /**
- * test one() with associations that are not entities.
- *
- * This can happen when request data is not completely marshalled.
- * incomplete associations should not cause warnings or fatal errors.
- *
- * @return void
- */
- public function testOneAssociationsNoEntities()
- {
- $class = 'TestApp\Model\Entity\ValidatableEntity';
- $article = $this->getMock($class, ['validate']);
- $comment1 = ['comment' => 'test'];
- $comment2 = ['comment' => 'omg'];
- $user = $this->getMock($class, ['validate']);
- $article->set('comments', [$comment1, $comment2]);
- $validator1 = $this->getMock('\Cake\Validation\Validator');
- $validator2 = $this->getMock('\Cake\Validation\Validator');
- $validator1->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- // Should not be called as comments are not entities.
- $validator2->expects($this->never())
- ->method('count');
- $this->articles->validator('default', $validator1);
- $this->comments->validator('default', $validator2);
- $entityValidator = new EntityValidator($this->articles);
- $article->expects($this->once())
- ->method('validate')
- ->with($validator1)
- ->will($this->returnValue([]));
- $options = ['associated' => ['Comments']];
- $this->assertFalse($entityValidator->one($article, $options));
- }
- /**
- * test one() with association data and one of them failing validation.
- *
- * @return void
- */
- public function testOneAssociationsFail()
- {
- $class = 'TestApp\Model\Entity\ValidatableEntity';
- $article = $this->getMock($class, ['validate']);
- $comment1 = $this->getMock($class, ['validate']);
- $comment2 = $this->getMock($class, ['validate']);
- $user = $this->getMock($class, ['validate']);
- $article->set('comments', [$comment1, $comment2]);
- $article->set('user', $user);
- $validator1 = $this->getMock('\Cake\Validation\Validator');
- $validator2 = $this->getMock('\Cake\Validation\Validator');
- $validator3 = $this->getMock('\Cake\Validation\Validator');
- $validator1->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- $validator2->expects($this->exactly(2))
- ->method('count')
- ->will($this->returnValue(1));
- $validator3->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- $this->articles->validator('default', $validator1);
- $this->comments->validator('default', $validator2);
- $this->users->validator('default', $validator3);
- $entityValidator = new EntityValidator($this->articles);
- $article->expects($this->once())
- ->method('validate')
- ->with($validator1)
- ->will($this->returnValue([]));
- $comment1->expects($this->once())
- ->method('validate')
- ->with($validator2)
- ->will($this->returnValue([]));
- $comment2->expects($this->once())
- ->method('validate')
- ->with($validator2)
- ->will($this->returnValue(['some' => ['error']]));
- $user->expects($this->once())
- ->method('validate')
- ->with($validator3)
- ->will($this->returnValue([]));
- $options = ['associated' => ['Comments', 'Users']];
- $this->assertFalse($entityValidator->one($article, $options));
- }
- /**
- * Test one() with deeper associations and passing the name for custom
- * validators
- *
- * @return void
- */
- public function testOneDeepAssociationsAndCustomValidators()
- {
- $class = 'TestApp\Model\Entity\ValidatableEntity';
- $comment = $this->getMock($class, ['validate']);
- $article = $this->getMock($class, ['validate']);
- $user = $this->getMock($class, ['validate']);
- $comment->set('article', $article);
- $article->set('user', $user);
- $validator1 = $this->getMock('\Cake\Validation\Validator');
- $validator2 = $this->getMock('\Cake\Validation\Validator');
- $validator3 = $this->getMock('\Cake\Validation\Validator');
- $validator1->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- $validator2->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- $validator3->expects($this->once())
- ->method('count')
- ->will($this->returnValue(1));
- $this->articles->validator('crazy', $validator1);
- $this->comments->validator('default', $validator2);
- $this->users->validator('funky', $validator3);
- $entityValidator = new EntityValidator($this->comments);
- $comment->expects($this->once())
- ->method('validate')
- ->with($validator2)
- ->will($this->returnValue([]));
- $article->expects($this->once())
- ->method('validate')
- ->with($validator1)
- ->will($this->returnValue([]));
- $user->expects($this->once())
- ->method('validate')
- ->with($validator3)
- ->will($this->returnValue([]));
- $this->assertTrue($entityValidator->one($comment, [
- 'associated' => [
- 'Articles' => [
- 'validate' => 'crazy',
- 'associated' => ['Users' => ['validate' => 'funky']]
- ]
- ]
- ]));
- }
- /**
- * Test many() with successful validate
- *
- * @return void
- */
- public function testManySuccess()
- {
- $class = 'TestApp\Model\Entity\ValidatableEntity';
- $comment1 = $this->getMock($class, ['validate']);
- $comment2 = $this->getMock($class, ['validate']);
- $validator = $this->getMock('\Cake\Validation\Validator');
- $data = [$comment1, $comment2];
- $this->comments->validator('default', $validator);
- $entityValidator = new EntityValidator($this->comments);
- $validator->expects($this->exactly(2))
- ->method('count')
- ->will($this->returnValue(1));
- $comment1->expects($this->once())
- ->method('validate')
- ->with($validator)
- ->will($this->returnValue([]));
- $comment2->expects($this->once())
- ->method('validate')
- ->with($validator)
- ->will($this->returnValue([]));
- $this->assertTrue($entityValidator->many($data));
- }
- /**
- * Test many() with failure
- *
- * @return void
- */
- public function testManyFailure()
- {
- $class = 'TestApp\Model\Entity\ValidatableEntity';
- $comment1 = $this->getMock($class, ['validate']);
- $comment2 = $this->getMock($class, ['validate']);
- $validator = $this->getMock('\Cake\Validation\Validator');
- $data = [$comment1, $comment2];
- $this->comments->validator('default', $validator);
- $entityValidator = new EntityValidator($this->comments);
- $validator->expects($this->exactly(2))
- ->method('count')
- ->will($this->returnValue(1));
- $comment1->expects($this->once())
- ->method('validate')
- ->with($validator)
- ->will($this->returnValue(['some' => ['error']]));
- $comment2->expects($this->once())
- ->method('validate')
- ->with($validator)
- ->will($this->returnValue([]));
- $this->assertFalse($entityValidator->many($data));
- }
- }
|