| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?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('\Cake\ORM\Entity', ['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(true));
- $this->assertTrue($entityValidator->one($entity));
- }
- /**
- * Test one() with failing validate
- *
- * @return void
- */
- public function testOneFail() {
- $entity = $this->getMock('\Cake\ORM\Entity', ['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(false));
- $this->assertFalse($entityValidator->one($entity));
- }
- /**
- * test one() with association data.
- *
- * @return void
- */
- public function testOneAssociationsSuccess() {
- $article = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $comment1 = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $comment2 = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $user = $this->getMock('\Cake\ORM\Entity', ['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(true));
- $comment1->expects($this->once())
- ->method('validate')
- ->with($validator2)
- ->will($this->returnValue(true));
- $comment2->expects($this->once())
- ->method('validate')
- ->with($validator2)
- ->will($this->returnValue(true));
- $user->expects($this->once())
- ->method('validate')
- ->with($validator3)
- ->will($this->returnValue(true));
- $options = ['associated' => ['Comments', 'Users']];
- $this->assertTrue($entityValidator->one($article, $options));
- }
- /**
- * test one() with association data and one of them failing validation.
- *
- * @return void
- */
- public function testOneAssociationsFail() {
- $article = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $comment1 = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $comment2 = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $user = $this->getMock('\Cake\ORM\Entity', ['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(true));
- $comment1->expects($this->once())
- ->method('validate')
- ->with($validator2)
- ->will($this->returnValue(true));
- $comment2->expects($this->once())
- ->method('validate')
- ->with($validator2)
- ->will($this->returnValue(false));
- $user->expects($this->once())
- ->method('validate')
- ->with($validator3)
- ->will($this->returnValue(true));
- $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() {
- $comment = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $article = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $user = $this->getMock('\Cake\ORM\Entity', ['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(true));
- $article->expects($this->once())
- ->method('validate')
- ->with($validator1)
- ->will($this->returnValue(true));
- $user->expects($this->once())
- ->method('validate')
- ->with($validator3)
- ->will($this->returnValue(true));
- $this->assertTrue($entityValidator->one($comment, [
- 'associated' => [
- 'Articles' => [
- 'validate' => 'crazy',
- 'associated' => ['Users' => ['validate' => 'funky']]
- ]
- ]
- ]));
- }
- /**
- * Test many() with successful validate
- *
- * @return void
- */
- public function testManySuccess() {
- $comment1 = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $comment2 = $this->getMock('\Cake\ORM\Entity', ['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(true));
- $comment2->expects($this->once())
- ->method('validate')
- ->with($validator)
- ->will($this->returnValue(true));
- $this->assertTrue($entityValidator->many($data));
- }
- /**
- * Test many() with failure
- *
- * @return void
- */
- public function testManyFailure() {
- $comment1 = $this->getMock('\Cake\ORM\Entity', ['validate']);
- $comment2 = $this->getMock('\Cake\ORM\Entity', ['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(false));
- $comment2->expects($this->once())
- ->method('validate')
- ->with($validator)
- ->will($this->returnValue(true));
- $this->assertFalse($entityValidator->many($data));
- }
- }
|