From 373bebb12834a2c1515337867a2f342bc0aa9e4f Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 29 Jul 2010 22:26:30 -0400 Subject: [PATCH] Updating test suite to properly handle parent_id situations. Fixes #881 --- .../cases/libs/model/behaviors/acl.test.php | 71 ++++++++++++++----- 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/cake/tests/cases/libs/model/behaviors/acl.test.php b/cake/tests/cases/libs/model/behaviors/acl.test.php index 980452c0835..a2469b051a1 100644 --- a/cake/tests/cases/libs/model/behaviors/acl.test.php +++ b/cake/tests/cases/libs/model/behaviors/acl.test.php @@ -90,14 +90,15 @@ function parentNode() { if (!$this->id && empty($this->data)) { return null; } - $data = $this->data; - if (empty($this->data)) { - $data = $this->read(); + if (isset($this->data['AclPerson']['mother_id'])) { + $motherId = $this->data['AclPerson']['mother_id']; + } else { + $motherId = $this->field('mother_id'); } - if (!$data['AclPerson']['mother_id']) { + if (!$motherId) { return null; } else { - return array('AclPerson' => array('id' => $data['AclPerson']['mother_id'])); + return array('AclPerson' => array('id' => $motherId)); } } } @@ -319,23 +320,60 @@ function testAfterSave() { 'foreign_key' => 1, 'parent_id' => null ) - ); - $this->Aro->create(); - $this->Aro->save($aroData); + ); + $this->Aro->create(); + $this->Aro->save($aroData); - $Person->read(null, 8); - $Person->set('mother_id', 1); - $Person->save(); - $result = $this->Aro->find('first', array( + $Person->read(null, 8); + $Person->set('mother_id', 1); + $Person->save(); + $result = $this->Aro->find('first', array( 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id) )); $this->assertTrue(is_array($result)); $this->assertEqual($result['Aro']['parent_id'], 7); - $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8)); - $this->assertEqual(sizeof($node), 2); - $this->assertEqual($node[0]['Aro']['parent_id'], 7); - $this->assertEqual($node[1]['Aro']['parent_id'], null); + $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8)); + $this->assertEqual(sizeof($node), 2); + $this->assertEqual($node[0]['Aro']['parent_id'], 7); + $this->assertEqual($node[1]['Aro']['parent_id'], null); + } + +/** + * test that an afterSave on an update does not cause parent_id to become null. + * + * @return void + */ + function testAfterSaveUpdateParentIdNotNull() { + $aroData = array( + 'Aro' => array( + 'model' => 'AclPerson', + 'foreign_key' => 2, + 'parent_id' => null + ) + ); + $this->Aro->save($aroData); + + $Person =& new AclPerson(); + $data = array( + 'AclPerson' => array( + 'name' => 'Trent', + 'mother_id' => 2, + 'father_id' => 3, + ), + ); + $Person->save($data); + $result = $this->Aro->find('first', array( + 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id) + )); + $this->assertTrue(is_array($result)); + $this->assertEqual($result['Aro']['parent_id'], 5); + + $Person->save(array('id' => $Person->id, 'name' => 'Bruce')); + $result = $this->Aro->find('first', array( + 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id) + )); + $this->assertEqual($result['Aro']['parent_id'], 5); } /** @@ -397,7 +435,6 @@ function testAfterDelete() { 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2) )); $this->assertTrue(empty($result)); - } /**