/**
  * testSaveHabtmNoPrimaryData method
  *
  * @return void
  */
 public function testSaveHabtmNoPrimaryData()
 {
     $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
     $TestModel = new Article();
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')), false);
     $result = $TestModel->findById(2);
     $expected = array('Article' => 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'), '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);
     $TestModel->id = 2;
     $data = array('Tag' => array('Tag' => array(2)));
     $result = $TestModel->save($data);
     $this->assertEquals($data['Tag'], $result['Tag']);
     $result = $TestModel->findById(2);
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => self::date()), 'Tag' => array(array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')));
     $this->assertEquals($expected, $result);
     $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio');
     $TestModel = new Portfolio();
     $result = $TestModel->findById(2);
     $expected = array('Portfolio' => array('id' => 2, 'seller_id' => 1, 'name' => 'Portfolio 2'), 'Item' => array(array('id' => 2, 'syfile_id' => 2, 'published' => '', 'name' => 'Item 2', 'ItemsPortfolio' => array('id' => 2, 'item_id' => 2, 'portfolio_id' => 2)), array('id' => 6, 'syfile_id' => 6, 'published' => '', 'name' => 'Item 6', 'ItemsPortfolio' => array('id' => 6, 'item_id' => 6, 'portfolio_id' => 2))));
     $this->assertEquals($expected, $result);
     $data = array('Item' => array('Item' => array(1, 2)));
     $TestModel->id = 2;
     $TestModel->save($data);
     $result = $TestModel->findById(2);
     $result['Item'] = Hash::sort($result['Item'], '{n}.id', 'asc');
     $expected = array('Portfolio' => array('id' => 2, 'seller_id' => 1, 'name' => 'Portfolio 2'), 'Item' => array(array('id' => 1, 'syfile_id' => 1, 'published' => '', 'name' => 'Item 1', 'ItemsPortfolio' => array('id' => 7, 'item_id' => 1, 'portfolio_id' => 2)), array('id' => 2, 'syfile_id' => 2, 'published' => '', 'name' => 'Item 2', 'ItemsPortfolio' => array('id' => 8, 'item_id' => 2, 'portfolio_id' => 2))));
     $this->assertEquals($expected, $result);
 }
 /**
  * undocumented function
  *
  * @return void
  */
 public function testBindModelCallsInBehaviors()
 {
     // hasMany
     $Article = new Article();
     $Article->unbindModel(array('hasMany' => array('Comment')));
     $result = $Article->find('first');
     $this->assertFalse(array_key_exists('Comment', $result));
     $Article->Behaviors->attach('Test4');
     $result = $Article->find('first');
     $this->assertTrue(array_key_exists('Comment', $result));
     // belongsTo
     $Article->unbindModel(array('belongsTo' => array('User')));
     $result = $Article->find('first');
     $this->assertFalse(array_key_exists('User', $result));
     $Article->Behaviors->attach('Test5');
     $result = $Article->find('first');
     $this->assertTrue(array_key_exists('User', $result));
     // hasAndBelongsToMany
     $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
     $result = $Article->find('first');
     $this->assertFalse(array_key_exists('Tag', $result));
     $Article->Behaviors->attach('Test6');
     $result = $Article->find('first');
     $this->assertTrue(array_key_exists('Comment', $result));
     // hasOne
     $Comment = new Comment();
     $Comment->unbindModel(array('hasOne' => array('Attachment')));
     $result = $Comment->find('first');
     $this->assertFalse(array_key_exists('Attachment', $result));
     $Comment->Behaviors->attach('Test7');
     $result = $Comment->find('first');
     $this->assertTrue(array_key_exists('Attachment', $result));
 }
 /**
  * test that a plugin model as the 'with' model doesn't have issues
  *
  * @return void
  */
 public function testDeleteLinksWithPLuginJoinModel()
 {
     $this->loadFixtures('Article', 'ArticlesTag', 'Tag');
     $Article = new Article();
     $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')), false);
     unset($Article->Tag, $Article->ArticleTags);
     $Article->bindModel(array('hasAndBelongsToMany' => array('Tag' => array('with' => 'TestPlugin.ArticlesTag'))), false);
     $this->assertTrue($Article->delete(1));
 }
Example #4
0
 /**
  * testSaveHabtm method
  *
  * @access public
  * @return void
  */
 public function testSaveHabtm()
 {
     $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
     $TestModel = new Article();
     $result = $TestModel->findById(2);
     $expected = array('Article' => 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->assertEqual($expected, $result);
     $data = array('Article' => array('id' => '2', 'title' => 'New Second Article'), 'Tag' => array('Tag' => array(1, 2)));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')));
     $this->assertEqual($expected, $result);
     $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEqual($expected, $result);
     $data = array('Tag' => array('Tag' => array(1, 2, 3)));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEqual($expected, $result);
     $data = array('Tag' => array('Tag' => array()));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $data = array('Tag' => array('Tag' => ''));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array());
     $this->assertEqual($expected, $result);
     $data = array('Tag' => array('Tag' => array(2, 3)));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEqual($expected, $result);
     $data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article'));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')));
     $this->assertEqual($expected, $result);
     $data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article Title'));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article Title', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')));
     $this->assertEqual($expected, $result);
     $data = array('Tag' => array('Tag' => array(2, 3)), 'Article' => array('id' => '2', 'title' => 'Changed Second Article'));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Changed Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEqual($expected, $result);
     $data = array('Tag' => array('Tag' => array(1, 3)), 'Article' => array('id' => '2'));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Changed Second Article', 'body' => 'Second Article Body'), '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->assertEqual($expected, $result);
     $data = array('Article' => array('id' => 10, 'user_id' => '2', 'title' => 'New Article With Tags and fieldList', 'body' => 'New Article Body with Tags and fieldList', 'created' => '2007-03-18 14:55:23', 'updated' => '2007-03-18 14:57:31'), 'Tag' => array('Tag' => array(1, 2, 3)));
     $result = $TestModel->create() && $TestModel->save($data, true, array('user_id', 'title', 'published'));
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->read();
     $expected = array('Article' => array('id' => 4, 'user_id' => 2, 'title' => 'New Article With Tags and fieldList', 'body' => '', 'published' => 'N', 'created' => '', 'updated' => ''), 'Tag' => array(0 => array('id' => 1, 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), 1 => array('id' => 2, 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), 2 => array('id' => 3, 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEqual($expected, $result);
     $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
     $TestModel = new JoinA();
     $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
     $data = array('JoinA' => array('id' => 1, 'name' => 'Join A 1', 'body' => 'Join A 1 Body'), 'JoinC' => array('JoinC' => array(array('join_c_id' => 2, 'other' => 'new record'), array('join_c_id' => 3, 'other' => 'new record'))));
     $TestModel->save($data);
     $result = $TestModel->read(null, 1);
     $expected = array(4, 5);
     $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/id', $result), $expected);
     $expected = array('new record', 'new record');
     $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/other', $result), $expected);
 }