/**
  * Test that afterFind is called correctly for 'hasOne' association.
  *
  * @return void
  */
 public function testHasOneAfterFind()
 {
     $this->loadFixtures('Article', 'User', 'Comment');
     $User = new User();
     $User->bindModel(array('hasOne' => array('Article')));
     $Article = $this->getMock('Article', array('afterFind'), array(), '', true);
     $Article->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment'), 'hasAndBelongsToMany' => array('Tag')));
     $Article->bindModel(array('hasOne' => array('Comment')));
     $Article->expects($this->once())->method('afterFind')->with($this->equalTo(array(0 => array('Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31', 'Comment' => array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'))))), $this->isFalse())->will($this->returnArgument(0));
     $User->Article = $Article;
     $User->find('first', array('conditions' => array('User.id' => 1), 'recursive' => 2));
 }
 /**
  * testRecursiveRead method
  *
  * @return void
  */
 public function testRecursiveRead()
 {
     $this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Featured', 'ArticleFeatured');
     $TestModel = new User();
     $result = $TestModel->bindModel(array('hasMany' => array('Article')), false);
     $this->assertTrue($result);
     $TestModel->recursive = 0;
     $result = $TestModel->read('id, user', 1);
     $expected = array('User' => array('id' => '1', 'user' => 'mariano'));
     $this->assertEquals($expected, $result);
     $TestModel->recursive = 1;
     $result = $TestModel->read('id, user', 1);
     $expected = array('User' => array('id' => '1', 'user' => 'mariano'), 'Article' => array(array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'), array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')));
     $this->assertEquals($expected, $result);
     $TestModel->recursive = 2;
     $result = $TestModel->read('id, user', 3);
     $expected = array('User' => array('id' => '3', 'user' => 'larry'), 'Article' => array(array('id' => '2', 'user_id' => '3', 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31', 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'), 'Comment' => array(array('id' => '5', 'article_id' => '2', 'user_id' => '1', 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'), array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')))));
     $this->assertEquals($expected, $result);
 }
 /**
  * Tests that altering data in a beforeValidate callback will lead to saving those
  * values in database
  *
  * @return void
  */
 public function testValidateFirstAssociatedWithBeforeValidate()
 {
     $this->loadFixtures('Article', 'User');
     $model = new CustomArticle();
     $model->validate = array('title' => array('notempty' => array('rule' => 'notEmpty', 'required' => true)));
     $articles = array(array('body' => 'foo1'), array('body' => 'foo2'), array('body' => 'foo3'));
     $user = new User();
     $user->bindModel(array('hasMany' => array('CustomArticle')));
     $data = array('User' => array('user' => 'foo', 'password' => 'bar'), 'CustomArticle' => $articles);
     $result = $user->saveAll($data, array('validate' => 'first'));
     $this->assertTrue($result);
     $this->assertEquals('foo', $model->field('title', array('body' => 'foo1')));
     $this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
     $this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
 }