Browse Source

Removing some validation logic out of the Table object

Jose Lorenzo Rodriguez 11 years ago
parent
commit
604b336110
3 changed files with 7 additions and 583 deletions
  1. 2 8
      src/ORM/Table.php
  2. 1 0
      tests/TestCase/ORM/MarshallerTest.php
  3. 4 575
      tests/TestCase/ORM/TableTest.php

+ 2 - 8
src/ORM/Table.php

@@ -1178,7 +1178,6 @@ class Table implements RepositoryInterface, EventListenerInterface {
 	public function save(EntityInterface $entity, $options = []) {
 		$options = new \ArrayObject($options + [
 			'atomic' => true,
-			'validate' => true,
 			'associated' => true
 		]);
 
@@ -1223,11 +1222,6 @@ class Table implements RepositoryInterface, EventListenerInterface {
 		}
 
 		$options['associated'] = $this->_associations->normalizeKeys($options['associated']);
-		$validate = $options['validate'];
-
-		if ($validate && !$this->validate($entity, $options)) {
-			return false;
-		}
 		$event = $this->dispatchEvent('Model.beforeSave', compact('entity', 'options'));
 
 		if ($event->isStopped()) {
@@ -1238,7 +1232,7 @@ class Table implements RepositoryInterface, EventListenerInterface {
 			$this,
 			$entity,
 			$options['associated'],
-			['validate' => false] + $options->getArrayCopy()
+			$options->getArrayCopy()
 		);
 
 		if (!$saved && $options['atomic']) {
@@ -1259,7 +1253,7 @@ class Table implements RepositoryInterface, EventListenerInterface {
 				$this,
 				$entity,
 				$options['associated'],
-				['validate' => (bool)$validate] + $options->getArrayCopy()
+				$options->getArrayCopy()
 			);
 			if ($success || !$options['atomic']) {
 				$entity->clean();

+ 1 - 0
tests/TestCase/ORM/MarshallerTest.php

@@ -1491,4 +1491,5 @@ class MarshallerTest extends TestCase {
 		$this->assertTrue($entity->tags[1]->dirty('_joinData'));
 	}
 
+
 }

+ 4 - 575
tests/TestCase/ORM/TableTest.php

@@ -2057,209 +2057,6 @@ class TableTest extends TestCase {
 	}
 
 /**
- * Tests saving with validation
- *
- * @return void
- */
-	public function testSaveWithValidationError() {
-		$entity = new \Cake\ORM\Entity([
-			'username' => 'superuser'
-		]);
-		$table = TableRegistry::get('users');
-		$table->validator()->requirePresence('password');
-		$this->assertFalse($table->save($entity));
-		$this->assertNotEmpty($entity->errors('password'));
-		$this->assertSame($entity, $table->validator()->provider('entity'));
-		$this->assertSame($table, $table->validator()->provider('table'));
-	}
-
-/**
- * Tests saving with validation and field list
- *
- * @return void
- */
-	public function testSaveWithValidationErrorAndFieldList() {
-		$entity = new \Cake\ORM\Entity([
-			'username' => 'superuser'
-		]);
-		$table = TableRegistry::get('users');
-		$table->validator()->requirePresence('password');
-		$this->assertFalse($table->save($entity));
-		$this->assertNotEmpty($entity->errors('password'));
-	}
-
-/**
- * Tests using a custom validation object when saving
- *
- * @return void
- */
-	public function testSaveWithDifferentValidator() {
-		$entity = new \Cake\ORM\Entity([
-			'username' => 'superuser'
-		]);
-		$table = TableRegistry::get('users');
-		$validator = (new Validator)->requirePresence('password');
-		$table->validator('custom', $validator);
-		$this->assertFalse($table->save($entity, ['validate' => 'custom']));
-		$this->assertNotEmpty($entity->errors('password'));
-
-		$this->assertSame($entity, $table->save($entity), 'default was not used');
-	}
-
-/**
- * Tests saving with successful validation
- *
- * @return void
- */
-	public function testSaveWithValidationSuccess() {
-		$entity = new \Cake\ORM\Entity([
-			'username' => 'superuser',
-			'password' => 'hey'
-		]);
-		$table = TableRegistry::get('users');
-		$table->validator()->requirePresence('password');
-		$this->assertSame($entity, $table->save($entity));
-		$this->assertEmpty($entity->errors('password'));
-	}
-
-/**
- * Tests beforeValidate event is triggered
- *
- * @return void
- */
-	public function testBeforeValidate() {
-		$entity = new \Cake\ORM\Entity([
-			'username' => 'superuser'
-		]);
-		$table = TableRegistry::get('users');
-		$table->eventManager()->attach(function ($ev, $en, $opt, $val) use ($entity) {
-			$this->assertSame($entity, $en);
-			$this->assertTrue($opt['crazy']);
-			$this->assertSame($ev->subject()->validator('default'), $val);
-			$val->requirePresence('password');
-		}, 'Model.beforeValidate');
-		$this->assertFalse($table->save($entity, ['crazy' => true]));
-		$this->assertNotEmpty($entity->errors('password'));
-	}
-
-/**
- * Tests that beforeValidate can set the validation result
- *
- * @return void
- */
-	public function testBeforeValidateSetResult() {
-		$entity = new \Cake\ORM\Entity([
-			'username' => 'superuser'
-		]);
-		$table = TableRegistry::get('users');
-		$table->eventManager()->attach(function ($ev, $en) {
-			$en->errors('username', 'Not good');
-			return false;
-		}, 'Model.beforeValidate');
-		$this->assertFalse($table->save($entity));
-		$this->assertEquals(['Not good'], $entity->errors('username'));
-	}
-
-/**
- * Tests that afterValidate is triggered and can set a result
- *
- * @return void
- */
-	public function testAfterValidate() {
-		$entity = new \Cake\ORM\Entity([
-			'username' => 'superuser',
-			'password' => 'hey'
-		]);
-		$table = TableRegistry::get('users');
-		$table->validator()->requirePresence('password');
-		$table->eventManager()->attach(function ($ev, $en, $opt, $val) use ($entity) {
-			$this->assertSame($entity, $en);
-			$this->assertTrue($opt['crazy']);
-			$this->assertSame($ev->subject()->validator('default'), $val);
-
-			$en->errors('username', 'Not good');
-			return false;
-		}, 'Model.afterValidate');
-
-		$this->assertFalse($table->save($entity, ['crazy' => true]));
-		$this->assertEmpty($entity->errors('password'));
-		$this->assertEquals(['Not good'], $entity->errors('username'));
-	}
-
-/**
- * Tests using a custom validation object when saving and saving associations
- *
- * @return void
- */
-	public function testSaveWithDifferentValidatorAndAssociations() {
-		$entity = new \Cake\ORM\Entity([
-			'title' => 'foo',
-			'body' => 'bar',
-			'author' => new \Cake\ORM\Entity([
-				'name' => 'Susan'
-			]),
-			'articles_tags' => [
-				new \Cake\ORM\Entity([
-					'tag_id' => 100
-				])
-			]
-		]);
-		$table = TableRegistry::get('articles');
-		$table->belongsTo('authors');
-		$table->hasMany('ArticlesTags');
-		$validator = (new Validator)->requirePresence('body');
-		$table->validator('custom', $validator);
-
-		$validator2 = (new Validator)->requirePresence('thing');
-		$table->authors->validator('default', $validator2);
-		$this->assertFalse($table->save($entity, ['validate' => 'custom']), 'default was not used');
-		$this->assertNotEmpty($entity->author->errors('thing'));
-
-		$table->ArticlesTags->validator('default', $validator2);
-		unset($entity->author);
-		$this->assertFalse($table->save($entity, ['validate' => 'custom']), 'default was not used');
-		$this->assertNotEmpty($entity->articles_tags[0]->errors('thing'));
-	}
-
-/**
- * Tests that save validates all entities before persisting.
- *
- * @return void
- */
-	public function testSaveValidateAllAssociations() {
-		$entity = new \Cake\ORM\Entity([
-			'title' => 'foo',
-			'author' => new \Cake\ORM\Entity([
-				'name' => 'Susan'
-			]),
-			'comments' => [
-				new \Cake\ORM\Entity([
-					'comment' => 'the worst!'
-				])
-			]
-		]);
-		$table = TableRegistry::get('Articles');
-		$table->belongsTo('Authors');
-		$table->hasMany('Comments');
-
-		$validator = (new Validator)->requirePresence('body');
-		$table->validator('default', $validator);
-
-		$authorValidate = (new Validator)->requirePresence('bio');
-		$table->Authors->validator('default', $authorValidate);
-
-		$commentValidate = (new Validator)->requirePresence('author');
-		$table->Comments->validator('default', $commentValidate);
-
-		$result = $table->save($entity);
-		$this->assertFalse($result, 'Should have failed');
-
-		$this->assertNotEmpty($entity->errors('body'), 'Missing article errors');
-		$this->assertNotEmpty($entity->author->errors('bio'), 'Missing author errors');
-		$this->assertNotEmpty($entity->comments[0]->errors('author'), 'Missing comment errors');
-	}
-
-/**
  * Test magic findByXX method.
  *
  * @return void
@@ -2452,35 +2249,6 @@ class TableTest extends TestCase {
 	}
 
 /**
- * Tests saving belongsTo association and get a validation error
- *
- * @group save
- * @return void
- */
-	public function testsSaveBelongsToWithValidationError() {
-		$entity = new \Cake\ORM\Entity([
-			'title' => 'A Title',
-			'body' => 'A body'
-		]);
-		$entity->author = new \Cake\ORM\Entity([
-			'name' => 'Jose'
-		]);
-
-		$table = TableRegistry::get('articles');
-		$table->belongsTo('authors');
-		$table->association('authors')
-			->target()
-			->validator()
-			->add('name', 'num', ['rule' => 'numeric']);
-
-		$this->assertFalse($table->save($entity));
-		$this->assertTrue($entity->isNew());
-		$this->assertTrue($entity->author->isNew());
-		$this->assertNull($entity->get('author_id'));
-		$this->assertNotEmpty($entity->author->errors('name'));
-	}
-
-/**
  * Tests saving hasOne association
  *
  * @group save
@@ -2526,43 +2294,12 @@ class TableTest extends TestCase {
 		$table = TableRegistry::get('authors');
 		$table->hasOne('articles');
 
-		$this->assertFalse($table->save($entity));
-		$this->assertTrue($entity->isNew());
+		$table->save($entity);
+		$this->assertFalse($entity->isNew());
 		$this->assertInternalType('array', $entity->article);
 	}
 
 /**
- * Tests saving hasOne association and returning a validation error will
- * abort the saving process
- *
- * @group save
- * @return void
- */
-	public function testSaveHasOneWithValidationError() {
-		$entity = new \Cake\ORM\Entity([
-			'name' => 'Jose'
-		]);
-		$entity->article = new \Cake\ORM\Entity([
-			'title' => 'A Title',
-			'body' => 'A body'
-		]);
-
-		$table = TableRegistry::get('authors');
-		$table->hasOne('articles');
-		$table->association('articles')
-			->target()
-			->validator()
-			->add('title', 'num', ['rule' => 'numeric']);
-		$this->assertFalse($table->save($entity));
-		$this->assertTrue($entity->isNew());
-		$this->assertTrue($entity->article->isNew());
-		$this->assertNull($entity->article->id);
-		$this->assertNull($entity->article->get('author_id'));
-		$this->assertFalse($entity->article->dirty('author_id'));
-		$this->assertNotEmpty($entity->article->errors('title'));
-	}
-
-/**
  * Tests saving multiple entities in a hasMany association
  *
  * @return void
@@ -2595,151 +2332,6 @@ class TableTest extends TestCase {
 	}
 
 /**
- * Tests saving multiple entities in a hasMany association and getting and
- * error while saving one of them. It should abort all the save operation
- * when options are set to defaults
- *
- * @return void
- */
-	public function testSaveHasManyWithErrorsAtomic() {
-		$entity = new \Cake\ORM\Entity([
-			'name' => 'Jose'
-		]);
-		$entity->articles = [
-			new \Cake\ORM\Entity([
-				'title' => '1',
-				'body' => 'A body'
-			]),
-			new \Cake\ORM\Entity([
-				'title' => 'Another Title',
-				'body' => 'Another body'
-			])
-		];
-
-		$table = TableRegistry::get('authors');
-		$table->hasMany('articles');
-		$table->association('articles')
-			->target()
-			->validator()
-			->add('title', 'num', ['rule' => 'numeric']);
-
-		$this->assertFalse($table->save($entity));
-		$this->assertTrue($entity->isNew());
-		$this->assertTrue($entity->articles[0]->isNew());
-		$this->assertTrue($entity->articles[1]->isNew());
-		$this->assertNull($entity->articles[0]->id);
-		$this->assertNull($entity->articles[1]->id);
-		$this->assertNull($entity->articles[0]->author_id);
-		$this->assertNull($entity->articles[1]->author_id);
-		$this->assertEmpty($entity->articles[0]->errors());
-		$this->assertNotEmpty($entity->articles[1]->errors());
-	}
-
-/**
- * Tests that it is possible to continue saving hasMany associations
- * even if any of the records fail validation when atomic is set
- * to false
- *
- * @return void
- */
-	public function testSaveHasManyWithErrorsNonAtomic() {
-		$entity = new \Cake\ORM\Entity([
-			'name' => 'Jose'
-		]);
-		$entity->articles = [
-			new \Cake\ORM\Entity([
-				'title' => 'A title',
-				'body' => 'A body'
-			]),
-			new \Cake\ORM\Entity([
-				'title' => '1',
-				'body' => 'Another body'
-			])
-		];
-
-		$table = TableRegistry::get('authors');
-		$table->hasMany('articles');
-		$table->association('articles')
-			->target()
-			->validator()
-			->add('title', 'num', ['rule' => 'numeric']);
-
-		$result = $table->save($entity, ['atomic' => false]);
-		$this->assertFalse($result, 'Validation failed, no save.');
-		$this->assertTrue($entity->isNew());
-		$this->assertTrue($entity->articles[0]->isNew());
-		$this->assertTrue($entity->articles[1]->isNew());
-		$this->assertNull($entity->articles[1]->id);
-		$this->assertNull($entity->articles[0]->id);
-		$this->assertNull($entity->articles[0]->author_id);
-		$this->assertNull($entity->articles[1]->author_id);
-	}
-
-/**
- * Tests saving hasOne association and returning a validation error will
- * not abort the saving process if atomic is set to false
- *
- * @group save
- * @return void
- */
-	public function testSaveHasOneWithValidationErrorNonAtomic() {
-		$entity = new \Cake\ORM\Entity([
-			'name' => 'Jose'
-		]);
-		$entity->article = new \Cake\ORM\Entity([
-			'title' => 'A Title',
-			'body' => 'A body'
-		]);
-
-		$table = TableRegistry::get('authors');
-		$table->hasOne('articles');
-		$table->association('articles')
-			->target()
-			->validator()
-			->add('title', 'num', ['rule' => 'numeric']);
-
-		$result = $table->save($entity, ['atomic' => false]);
-		$this->assertFalse($result, 'Validation failed, no save.');
-		$this->assertTrue($entity->isNew());
-		$this->assertTrue($entity->article->isNew());
-		$this->assertNull($entity->article->id);
-		$this->assertNull($entity->article->get('author_id'));
-		$this->assertFalse($entity->article->dirty('author_id'));
-		$this->assertNotEmpty($entity->article->errors('title'));
-	}
-
-/**
- * Tests saving belongsTo association and get a validation error won't stop
- * saving if atomic is set to false
- *
- * @group save
- * @return void
- */
-	public function testSaveBelongsToWithValidationErrorNotAtomic() {
-		$entity = new \Cake\ORM\Entity([
-			'title' => 'A Title',
-			'body' => 'A body'
-		]);
-		$entity->author = new \Cake\ORM\Entity([
-			'name' => 'Jose'
-		]);
-
-		$table = TableRegistry::get('articles');
-		$table->belongsTo('authors');
-		$table->association('authors')
-			->target()
-			->validator()
-			->add('name', 'num', ['rule' => 'numeric']);
-
-		$result = $table->save($entity, ['atomic' => false]);
-		$this->assertFalse($result, 'Validation failed, no save');
-		$this->assertTrue($entity->isNew());
-		$this->assertTrue($entity->author->isNew());
-		$this->assertNull($entity->get('author_id'));
-		$this->assertNotEmpty($entity->author->errors('name'));
-	}
-
-/**
  * Tests saving belongsToMany records
  *
  * @group save
@@ -2873,159 +2465,6 @@ class TableTest extends TestCase {
 	}
 
 /**
- * Tests saving belongsToMany records with a validation error and atomic set
- * to true
- *
- * @group save
- * @return void
- */
-	public function testSaveBelongsToManyWithValidationErrorAtomic() {
-		$entity = new \Cake\ORM\Entity([
-			'title' => 'A Title',
-			'body' => 'A body'
-		]);
-		$entity->tags = [
-			new \Cake\ORM\Entity([
-				'name' => '100'
-			]),
-			new \Cake\ORM\Entity([
-				'name' => 'Something New'
-			])
-		];
-		$table = TableRegistry::get('articles');
-		$table->belongsToMany('tags');
-		$tags = $table->association('tags')
-			->target()
-			->validator()
-			->add('name', 'num', ['rule' => 'numeric']);
-
-		$this->assertFalse($table->save($entity));
-		$this->assertTrue($entity->isNew());
-		$this->assertTrue($entity->tags[0]->isNew());
-		$this->assertTrue($entity->tags[1]->isNew());
-		$this->assertNull($entity->tags[0]->id);
-		$this->assertNull($entity->tags[1]->id);
-		$this->assertNull($entity->tags[0]->_joinData);
-		$this->assertNull($entity->tags[1]->_joinData);
-		$this->assertEmpty($entity->tags[0]->errors('name'));
-		$this->assertNotEmpty($entity->tags[1]->errors('name'));
-	}
-
-/**
- * Tests saving belongsToMany records with a validation error and atomic set
- * to false
- *
- * @group save
- * @return void
- */
-	public function testSaveBelongsToManyWithValidationErrorNonAtomic() {
-		$entity = new \Cake\ORM\Entity([
-			'title' => 'A Title',
-			'body' => 'A body'
-		]);
-		$entity->tags = [
-			new \Cake\ORM\Entity([
-				'name' => 'Something New'
-			]),
-			new \Cake\ORM\Entity([
-				'name' => '100'
-			])
-		];
-		$table = TableRegistry::get('articles');
-		$table->belongsToMany('tags');
-		$tags = $table->association('tags')
-			->target()
-			->validator()
-			->add('name', 'num', ['rule' => 'numeric']);
-
-		$result = $table->save($entity, ['atomic' => false]);
-
-		$this->assertFalse($result, 'HABTM validation failed, save aborted');
-		$this->assertTrue($entity->isNew());
-		$this->assertTrue($entity->tags[0]->isNew());
-		$this->assertTrue($entity->tags[1]->isNew());
-		$this->assertNull($entity->tags[0]->id);
-		$this->assertNull($entity->tags[1]->id);
-		$this->assertNull($entity->tags[0]->_joinData);
-		$this->assertNull($entity->tags[1]->_joinData);
-	}
-
-/**
- * Tests saving belongsToMany records with a validation error in a joint entity
- *
- * @group save
- * @return void
- */
-	public function testSaveBelongsToManyWithValidationErrorInJointEntity() {
-		$entity = new \Cake\ORM\Entity([
-			'title' => 'A Title',
-			'body' => 'A body'
-		]);
-		$entity->tags = [
-			new \Cake\ORM\Entity([
-				'name' => 'Something New'
-			]),
-			new \Cake\ORM\Entity([
-				'name' => '100'
-			])
-		];
-		$table = TableRegistry::get('articles');
-		$table->belongsToMany('tags');
-		$table->association('tags')
-			->junction()
-			->validator()
-			->add('article_id', 'num', ['rule' => ['comparison', '>', 4]]);
-
-		$this->assertFalse($table->save($entity));
-		$this->assertTrue($entity->isNew());
-		$this->assertTrue($entity->tags[0]->isNew());
-		$this->assertTrue($entity->tags[1]->isNew());
-		$this->assertNull($entity->tags[0]->id);
-		$this->assertNull($entity->tags[1]->id);
-		$this->assertNull($entity->tags[0]->_joinData);
-		$this->assertNull($entity->tags[1]->_joinData);
-	}
-
-/**
- * Tests saving belongsToMany records with a validation error in a joint entity
- * and atomic set to false
- *
- * @group save
- * @return void
- */
-	public function testSaveBelongsToManyWithValidationErrorInJointEntityNonAtomic() {
-		$entity = new \Cake\ORM\Entity([
-			'title' => 'A Title',
-			'body' => 'A body'
-		]);
-		$entity->tags = [
-			new \Cake\ORM\Entity([
-				'name' => 'Something New'
-			]),
-			new \Cake\ORM\Entity([
-				'name' => 'New one'
-			])
-		];
-		$table = TableRegistry::get('articles');
-		$table->belongsToMany('tags');
-		$table->association('tags')
-			->junction()
-			->validator()
-			->add('tag_id', 'num', ['rule' => ['comparison', '>', 4]]);
-
-		$this->assertSame($entity, $table->save($entity, ['atomic' => false]));
-		$this->assertFalse($entity->isNew());
-		$this->assertFalse($entity->tags[0]->isNew());
-		$this->assertFalse($entity->tags[1]->isNew());
-		$this->assertEquals(4, $entity->tags[0]->id);
-		$this->assertEquals(5, $entity->tags[1]->id);
-		$this->assertTrue($entity->tags[0]->_joinData->isNew());
-		$this->assertNotEmpty($entity->tags[0]->_joinData->errors());
-		$this->assertEquals(4, $entity->tags[1]->_joinData->article_id);
-		$this->assertEquals(5, $entity->tags[1]->_joinData->tag_id);
-	}
-
-/**
  * Tests that saving a persisted and clean entity will is a no-op
  *
  * @group save
@@ -3079,7 +2518,7 @@ class TableTest extends TestCase {
 		);
 		$authors = $this->getMock(
 			'\Cake\ORM\Table',
-			['_insert', 'validate'],
+			['_insert'],
 			[['table' => 'authors', 'connection' => $this->connection]]
 		);
 		$supervisors = $this->getMock(
@@ -3126,28 +2565,18 @@ class TableTest extends TestCase {
 			->with($entity->author, ['name' => 'Juan'])
 			->will($this->returnValue($entity->author));
 
-		$authors->expects($this->once())
-			->method('validate')
-			->with($entity->author)
-			->will($this->returnValue(true));
-
 		$supervisors->expects($this->once())
 			->method('_insert')
 			->with($entity->author->supervisor, ['name' => 'Marc'])
 			->will($this->returnValue($entity->author->supervisor));
 
-		$supervisors->expects($this->never())->method('validate');
-
 		$tags->expects($this->never())->method('_insert');
 
 		$this->assertSame($entity, $articles->save($entity, [
 			'associated' => [
-				'authors' => [
-					'validate' => true
-				],
+				'authors' => [],
 				'authors.supervisors' => [
 					'atomic' => false,
-					'validate' => false,
 					'associated' => false
 				]
 			]