Making a bunch of properties public so containable can hack around. Need to refactor and fix visibility issues.

Fixing failing tests in containable.
This commit is contained in:
mark_story 2010-12-12 15:48:36 -05:00
parent 10c358742b
commit df21d19c4c
3 changed files with 47 additions and 22 deletions

View file

@ -96,7 +96,10 @@ class ContainableBehavior extends ModelBehavior {
*/
public function beforeFind($Model, $query) {
$reset = (isset($query['reset']) ? $query['reset'] : true);
$noContain = ((isset($this->runtime[$Model->alias]['contain']) && empty($this->runtime[$Model->alias]['contain'])) || (isset($query['contain']) && empty($query['contain'])));
$noContain = (
(isset($this->runtime[$Model->alias]['contain']) && empty($this->runtime[$Model->alias]['contain'])) ||
(isset($query['contain']) && empty($query['contain']))
);
$contain = array();
if (isset($this->runtime[$Model->alias]['contain'])) {
$contain = $this->runtime[$Model->alias]['contain'];
@ -105,7 +108,10 @@ class ContainableBehavior extends ModelBehavior {
if (isset($query['contain'])) {
$contain = array_merge($contain, (array)$query['contain']);
}
if ($noContain || !$contain || in_array($contain, array(null, false), true) || (isset($contain[0]) && $contain[0] === null)) {
if (
$noContain || !$contain || in_array($contain, array(null, false), true) ||
(isset($contain[0]) && $contain[0] === null)
) {
if ($noContain) {
$query['recursive'] = -1;
}
@ -177,6 +183,7 @@ class ContainableBehavior extends ModelBehavior {
$autoFields = ($this->settings[$Model->alias]['autoFields']
&& !in_array($Model->findQueryType, array('list', 'count'))
&& !empty($query['fields']));
if (!$autoFields) {
return $query;
}
@ -264,7 +271,7 @@ class ContainableBehavior extends ModelBehavior {
$Model->resetAssociations();
if (!empty($Model->__backInnerAssociation)) {
$assocs = $Model->__backInnerAssociation;
unset($Model->__backInnerAssociation);
$Model->__backInnerAssociation = array();
foreach ($assocs as $currentModel) {
$this->resetBindings($Model->$currentModel);
}

View file

@ -330,7 +330,13 @@ class Model extends Object {
* @var array
* @access private
*/
private $__backAssociation = array();
public $__backAssociation = array();
public $__backInnerAssociation = array();
public $__backOriginalAssociation = array();
public $__backContainableAssociation = array();
/**
* The ID of the model record that was last inserted.
@ -2139,6 +2145,7 @@ class Model extends Object {
array(&$this, $query),
array('break' => true, 'breakOn' => false, 'modParams' => 1)
);
$query = (is_array($return)) ? $return : $query;
if ($return === false) {

View file

@ -35,7 +35,8 @@ class ContainableBehaviorTest extends CakeTestCase {
* @access public
*/
public $fixtures = array(
'core.article', 'core.article_featured', 'core.article_featureds_tags', 'core.articles_tag', 'core.attachment', 'core.category',
'core.article', 'core.article_featured', 'core.article_featureds_tags',
'core.articles_tag', 'core.attachment', 'core.category',
'core.comment', 'core.featured', 'core.tag', 'core.user'
);
@ -43,10 +44,11 @@ class ContainableBehaviorTest extends CakeTestCase {
* Method executed before each test
*
*/
public function startTest() {
$this->User =& ClassRegistry::init('User');
$this->Article =& ClassRegistry::init('Article');
$this->Tag =& ClassRegistry::init('Tag');
public function setUp() {
parent::setUp();
$this->User = ClassRegistry::init('User');
$this->Article = ClassRegistry::init('Article');
$this->Tag = ClassRegistry::init('Tag');
$this->User->bindModel(array(
'hasMany' => array('Article', 'ArticleFeatured', 'Comment')
@ -67,12 +69,11 @@ class ContainableBehaviorTest extends CakeTestCase {
* Method executed after each test
*
*/
public function endTest() {
public function tearDown() {
unset($this->Article);
unset($this->User);
unset($this->Tag);
ClassRegistry::flush();
parent::tearDown();
}
/**
@ -116,15 +117,23 @@ class ContainableBehaviorTest extends CakeTestCase {
$r = $this->__containments($this->Article, array('Comment' => array('limit' => 1)));
$this->assertEqual(array_keys($r), array('Comment', 'Article'));
$this->assertEqual(array_shift(Set::extract('/Comment/keep', $r)), array('keep' => array()));
$result = Set::extract('/Comment/keep', $r);
$this->assertEqual(array_shift($result), array('keep' => array()));
$this->assertTrue(Set::matches('/Article/keep/Comment', $r));
$this->assertEqual(array_shift(Set::extract('/Article/keep/Comment/.', $r)), array('limit' => 1));
$result = Set::extract('/Article/keep/Comment/.', $r);
$this->assertEqual(array_shift($result), array('limit' => 1));
$r = $this->__containments($this->Article, array('Comment.User'));
$this->assertEqual(array_keys($r), array('User', 'Comment', 'Article'));
$this->assertEqual(array_shift(Set::extract('/User/keep', $r)), array('keep' => array()));
$this->assertEqual(array_shift(Set::extract('/Comment/keep', $r)), array('keep' => array('User' => array())));
$this->assertEqual(array_shift(Set::extract('/Article/keep', $r)), array('keep' => array('Comment' => array())));
$result = Set::extract('/User/keep', $r);
$this->assertEqual(array_shift($result), array('keep' => array()));
$result = Set::extract('/Comment/keep', $r);
$this->assertEqual(array_shift($result), array('keep' => array('User' => array())));
$result = Set::extract('/Article/keep', $r);
$this->assertEqual(array_shift($result), array('keep' => array('Comment' => array())));
$r = $this->__containments($this->Tag, array('Article' => array('User' => array('Comment' => array(
'Attachment' => array('conditions' => array('Attachment.id >' => 1))
@ -3189,10 +3198,11 @@ class ContainableBehaviorTest extends CakeTestCase {
* @return void
*/
function testPaginate() {
$Controller = new Controller();
App::import('Core', 'Controller');
$Controller = new Controller($this->getMock('CakeRequest'));
$Controller->uses = array('Article');
$Controller->passedArgs[] = '1';
$Controller->params['url'] = array();
$Controller->request->params['url'] = array();
$Controller->constructClasses();
$Controller->paginate = array('Article' => array('fields' => array('title'), 'contain' => array('User(user)')));
@ -3396,6 +3406,7 @@ class ContainableBehaviorTest extends CakeTestCase {
'conditions' => array('Article.id' => 1),
'contain' => array('ArticlesTag')
));
$expected = array('Article', 'ArticlesTag');
$this->assertTrue(!empty($result));
$this->assertEqual('First Article', $result['Article']['title']);
@ -3605,7 +3616,7 @@ class ContainableBehaviorTest extends CakeTestCase {
return;
}
$db =& ConnectionManager::getDataSource('test2');
$db = ConnectionManager::getDataSource('test2');
$this->_fixtures[$this->_fixtureClassMap['User']]->create($db);
$this->_fixtures[$this->_fixtureClassMap['User']]->insert($db);
@ -3641,7 +3652,7 @@ class ContainableBehaviorTest extends CakeTestCase {
$result = $this->Article->find('all', array(
'conditions' => array('Article.id' => 999999999)
));
$this->assertEqual($result, array(), 'Should be empty.');
$this->assertEmpty($result, 'Should be empty.');
}
/**