Browse Source

Removing all validation methods from Table

Jose Lorenzo Rodriguez 11 years ago
parent
commit
12fdc5ce42

+ 1 - 1
src/ORM/EntityValidator.php

@@ -99,7 +99,7 @@ class EntityValidator {
 				continue;
 			}
 
-			$validator = $association->target()->entityValidator();
+			$validator = new self($association->target());
 			if ($isOne) {
 				$valid = $validator->one($value, $assoc['options']) && $valid;
 			} else {

+ 0 - 114
src/ORM/Table.php

@@ -1636,18 +1636,6 @@ class Table implements RepositoryInterface, EventListenerInterface {
 	}
 
 /**
- * Returns a new instance of an EntityValidator that is configured to be used
- * for entities generated by this table. An EntityValidator can be used to
- * process validation rules on a single or multiple entities and any of its
- * associated values.
- *
- * @return EntityValidator
- */
-	public function entityValidator() {
-		return new EntityValidator($this);
-	}
-
-/**
  * {@inheritDoc}
  *
  * By default all the associations on this table will be hydrated. You can
@@ -1782,108 +1770,6 @@ class Table implements RepositoryInterface, EventListenerInterface {
 	}
 
 /**
- * Validates a single entity based on the passed options and validates
- * any nested entity for this table associations as requested in the
- * options array.
- *
- * Calling this function directly is mostly useful when you need to get
- * validation errors for an entity and associated nested entities before
- * they are saved.
- *
- * {{{
- * $articles->validate($article);
- * }}}
- *
- * You can specify which validation set to use using the options array:
- *
- * {{{
- * $users->validate($user, ['validate' => 'forSignup']);
- * }}}
- *
- * By default all the associations on this table will be validated if they can
- * be found in the passed entity. You can limit which associations are built,
- * or include deeper associations using the options parameter
- *
- * {{{
- * $articles->validate($article, [
- *	'associated' => [
- *		'Tags',
- *		'Comments' => [
- *			'validate' => 'myCustomSet',
- *			'associated' => ['Users']
- *		]
- *	]
- * ]);
- * }}}
- *
- * @param \Cake\Datasource\EntityInterface $entity The entity to be validated
- * @param array|\ArrayObject $options A list of options to use while validating, the following
- * keys are accepted:
- * - validate: The name of the validation set to use
- * - associated: map of association names to validate as well
- * @return bool true if the passed entity and its associations are valid
- */
-	public function validate($entity, $options = []) {
-		if (!isset($options['associated'])) {
-			$options['associated'] = $this->_associations->keys();
-		}
-
-		$entityValidator = $this->entityValidator();
-		return $entityValidator->one($entity, $options);
-	}
-
-/**
- * Validates a list of entities based on the passed options and validates
- * any nested entity for this table associations as requested in the
- * options array.
- *
- * Calling this function directly is mostly useful when you need to get
- * validation errors for a list of entities and associations before they are
- * saved.
- *
- * {{{
- * $articles->validateMany([$article1, $article2]);
- * }}}
- *
- * You can specify which validation set to use using the options array:
- *
- * {{{
- * $users->validateMany([$user1, $user2], ['validate' => 'forSignup']);
- * }}}
- *
- * By default all the associations on this table will be validated if they can
- * be found in the passed entities. You can limit which associations are built,
- * or include deeper associations using the options parameter
- *
- * {{{
- * $articles->validateMany([$article1, $article2], [
- *	'associated' => [
- *		'Tags',
- *		'Comments' => [
- *			'validate' => 'myCustomSet',
- *			'associated' => ['Users']
- *		]
- *	]
- * ]);
- * }}}
- *
- * @param array|\ArrayAccess $entities The entities to be validated
- * @param array $options A list of options to use while validating, the following
- * keys are accepted:
- * - validate: The name of the validation set to use
- * - associated: map of association names to validate as well
- * @return bool true if the passed entities and their associations are valid
- */
-	public function validateMany($entities, array $options = []) {
-		if (!isset($options['associated'])) {
-			$options['associated'] = $this->_associations->keys();
-		}
-
-		$entityValidator = $this->entityValidator();
-		return $entityValidator->many($entities, $options);
-	}
-
-/**
  * Validator method used to check the uniqueness of a value for a column.
  * This is meant to be used with the validation API and not to be called
  * directly.

+ 0 - 102
tests/TestCase/ORM/TableTest.php

@@ -2934,108 +2934,6 @@ class TableTest extends TestCase {
 	}
 
 /**
- * Tests entityValidator
- *
- * @return void
- */
-	public function testEntityValidator() {
-		$table = new Table;
-		$expected = new \Cake\ORM\EntityValidator($table);
-		$table->entityValidator();
-		$this->assertEquals($expected, $table->entityValidator());
-	}
-
-/**
- * Tests that validate will call the entity validator with the correct
- * options
- *
- * @return void
- */
-	public function testValidateDefaultAssociations() {
-		$table = $this->getMock('\Cake\ORM\Table', ['entityValidator']);
-		$table->belongsTo('users');
-		$table->hasMany('articles');
-		$table->schema([]);
-
-		$entityValidator = $this->getMock('\Cake\ORM\EntityValidator', [], [$table]);
-		$entity = $table->newEntity([]);
-
-		$table->expects($this->once())->method('entityValidator')
-			->will($this->returnValue($entityValidator));
-		$entityValidator->expects($this->once())->method('one')
-			->with($entity, ['associated' => ['users', 'articles']])
-			->will($this->returnValue(true));
-		$this->assertTrue($table->validate($entity));
-	}
-
-/**
- * Tests that validate will call the entity validator with the correct
- * options
- *
- * @return void
- */
-	public function testValidateWithCustomOptions() {
-		$table = $this->getMock('\Cake\ORM\Table', ['entityValidator']);
-		$table->schema([]);
-
-		$entityValidator = $this->getMock('\Cake\ORM\EntityValidator', [], [$table]);
-		$entity = $table->newEntity([]);
-		$options = ['associated' => ['users'], 'validate' => 'foo'];
-
-		$table->expects($this->once())->method('entityValidator')
-			->will($this->returnValue($entityValidator));
-		$entityValidator->expects($this->once())->method('one')
-			->with($entity, $options)
-			->will($this->returnValue(false));
-		$this->assertFalse($table->validate($entity, $options));
-	}
-
-/**
- * Tests that validateMany will call the entity validator with the correct
- * options
- *
- * @return void
- */
-	public function testValidateManyDefaultAssociation() {
-		$table = $this->getMock('\Cake\ORM\Table', ['entityValidator']);
-		$table->belongsTo('users');
-		$table->hasMany('articles');
-		$table->schema([]);
-
-		$entityValidator = $this->getMock('\Cake\ORM\EntityValidator', [], [$table]);
-		$entities = ['a', 'b'];
-
-		$table->expects($this->once())->method('entityValidator')
-			->will($this->returnValue($entityValidator));
-		$entityValidator->expects($this->once())->method('many')
-			->with($entities, ['associated' => ['users', 'articles']])
-			->will($this->returnValue(true));
-		$this->assertTrue($table->validateMany($entities));
-	}
-
-/**
- * Tests that validateMany will call the entity validator with the correct
- * options
- *
- * @return void
- */
-	public function testValidateManyWithCustomOptions() {
-		$table = $this->getMock('\Cake\ORM\Table', ['entityValidator']);
-		$table->schema([]);
-
-		$entityValidator = $this->getMock('\Cake\ORM\EntityValidator', [], [$table]);
-		$entities = ['a', 'b', 'c'];
-		$options = ['associated' => ['users'], 'validate' => 'foo'];
-
-		$table->expects($this->once())->method('entityValidator')
-			->will($this->returnValue($entityValidator));
-		$entityValidator->expects($this->once())->method('many')
-			->with($entities, $options)
-			->will($this->returnValue(false));
-		$this->assertFalse($table->validateMany($entities, $options));
-	}
-
-/**
  * Tests that patchEntity delegates the task to the marshaller and passed
  * all associations
  *

+ 0 - 104
tests/TestCase/ORM/ValidationIntegrationTest.php

@@ -1,104 +0,0 @@
-<?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\TableRegistry;
-use Cake\TestSuite\TestCase;
-
-/**
- * Tests the integration between the ORM and the validator
- */
-class ValidationIntegrationTest extends TestCase {
-
-/**
- * Fixtures to be loaded
- *
- * @var array
- */
-	public $fixtures = ['core.articles'];
-
-/**
- * Tear down
- *
- * @return void
- */
-	public function tearDown() {
-		parent::tearDown();
-		TableRegistry::clear();
-	}
-
-/**
- * Tests that Table::validateUnique handle validation params correctly
- *
- * @return void
- */
-	public function testValidateUnique() {
-		$articles = TableRegistry::get('articles');
-		$articles->validator()->add('title', [
-			'unique' => [
-				'rule' => 'validateUnique',
-				'provider' => 'table',
-				'message' => 'Y U NO WRITE UNIQUE?'
-			]
-		]);
-		$entity = new Entity(['title' => 'First Article', 'body' => 'Foo']);
-		$this->assertFalse($articles->validate($entity));
-		$this->assertEquals('Y U NO WRITE UNIQUE?', $entity->errors()['title']['unique']);
-
-		$entity = new Entity(['title' => 'New Article', 'body' => 'Foo']);
-		$this->assertTrue($articles->validate($entity));
-	}
-
-/**
- * Tests that validateUnique can be scoped to another field in the provided data
- *
- * @return void
- */
-	public function testValidateUniqueWithScope() {
-		$articles = TableRegistry::get('articles');
-		$articles->validator()->add('title', [
-			'unique' => [
-				'rule' => ['validateUnique', ['scope' => 'published']],
-				'provider' => 'table'
-			]
-		]);
-		$entity = new Entity(['title' => 'First Article', 'published' => 'N']);
-		$this->assertTrue($articles->validate($entity));
-
-		$entity->published = 'Y';
-		$this->assertFalse($articles->validate($entity));
-	}
-
-/**
- * Tests that uniqueness validation excludes the same record when it exists
- *
- * @return void
- */
-	public function testValidateUniqueUpdate() {
-		$articles = TableRegistry::get('articles');
-		$articles->validator()->add('title', [
-			'unique' => [
-				'rule' => 'validateUnique',
-				'provider' => 'table'
-			]
-		]);
-		$entity = new Entity(['id' => 1, 'title' => 'First Article'], ['markNew' => false]);
-		$this->assertTrue($articles->validate($entity));
-
-		$entity = new Entity(['id' => 2, 'title' => 'First Article'], ['markNew' => false]);
-		$this->assertFalse($articles->validate($entity));
-	}
-}