Permalink
Browse files

Fixing input generation for collections when prefixing table name

  • Loading branch information...
1 parent c0faabb commit 0119f79adff950ceb0e9361788d75feedec0af74 @lorenzo lorenzo committed Feb 27, 2014
@@ -217,6 +217,11 @@ protected function _getEntity($path) {
}
$lastProp = $this->_rootName;
+
+ if ($path[0] === $this->_rootName) {
+ $path = array_slice($path, 1);
+ }
+
foreach ($path as $prop) {
$next = $this->_getProp($entity, $prop);
if (
@@ -230,6 +230,34 @@ public function testValOnCollections($collection) {
$this->assertNull($context->val('99.title'));
}
+/**
+ * Test operations on a collection of entities when prefixing with the
+ * table name
+ *
+ * @dataProvider collectionProvider
+ * @return void
+ */
+ public function testValOnCollectionsWithRootName($collection) {
+ $context = new EntityContext($this->request, [
+ 'entity' => $collection,
+ 'table' => 'Articles',
+ ]);
+
+ $result = $context->val('Articles.0.title');
+ $this->assertEquals('First post', $result);
+
+ $result = $context->val('Articles.0.user.username');
+ $this->assertEquals('mark', $result);
+
+ $result = $context->val('Articles.1.title');
+ $this->assertEquals('Second post', $result);
+
+ $result = $context->val('Articles.1.user.username');
+ $this->assertEquals('jose', $result);
+
+ $this->assertNull($context->val('Articles.99.title'));
+ }
+
/**
* Test error operations on a collection of entities.
*
@@ -2030,13 +2030,10 @@ public function testEmptyInputErrorValidation() {
* @return void
*/
public function testFormValidationAssociated() {
- TableRegistry::get('Contacts', [
- 'className' => __NAMESPACE__ . '\ContactsTable'
- ]);
$nested = new Entity(['foo' => 'bar']);
$nested->errors('foo', ['not a valid bar']);
$entity = new Entity(['nested' => $nested]);
- $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
+ $this->Form->create($entity);
$result = $this->Form->error('nested.foo');
$this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
@@ -2050,14 +2047,11 @@ public function testFormValidationAssociated() {
* @return void
*/
public function testFormValidationAssociatedSecondLevel() {
- TableRegistry::get('Contacts', [
- 'className' => __NAMESPACE__ . '\ContactsTable'
- ]);
$inner = new Entity(['bar' => 'baz']);
$nested = new Entity(['foo' => $inner]);
$entity = new Entity(['nested' => $nested]);
$inner->errors('bar', ['not a valid one']);
- $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
+ $this->Form->create($entity);
$result = $this->Form->error('nested.foo.bar');
$this->assertEquals('<div class="error-message">not a valid one</div>', $result);
}
@@ -2070,22 +2064,25 @@ public function testFormValidationAssociatedSecondLevel() {
* @return void
*/
public function testFormValidationMultiRecord() {
- $this->markTestIncomplete('Need to revisit once models work again.');
- $Contact->validationErrors[2] = array(
- 'name' => array('The provided value is invalid')
- );
- $result = $this->Form->input('Contact.2.name');
+ $one = new Entity;
+ $two = new Entity;
+ TableRegistry::get('Contacts', [
+ 'className' => __NAMESPACE__ . '\ContactsTable'
+ ]);
+ $two->errors('name', ['This is wrong']);
+ $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
+ $result = $this->Form->input('Contacts.1.name');
$expected = array(
'div' => array('class' => 'input text error'),
- 'label' => array('for' => 'Contact2Name'),
+ 'label' => array('for' => 'contacts-1-name'),
'Name',
'/label',
'input' => array(
- 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
+ 'type' => 'text', 'name' => 'Contacts[1][name]', 'id' => 'contacts-1-name',
'class' => 'form-error', 'maxlength' => 255
),
array('div' => array('class' => 'error-message')),
- 'The provided value is invalid',
+ 'This is wrong',
'/div',
'/div'
);

0 comments on commit 0119f79

Please sign in to comment.