/** * test find('list') method * * @return void */ public function testGenerateFindList() { $this->loadFixtures('Article', 'Apple', 'Post', 'Author', 'User', 'Comment'); $TestModel = new Article(); $TestModel->displayField = 'title'; $result = $TestModel->find('list', array('order' => 'Article.title ASC')); $expected = array(1 => 'First Article', 2 => 'Second Article', 3 => 'Third Article'); $this->assertEquals($expected, $result); $db = ConnectionManager::getDataSource('test'); if ($db instanceof Mysql) { $result = $TestModel->find('list', array('order' => array('FIELD(Article.id, 3, 2) ASC', 'Article.title ASC'))); $expected = array(1 => 'First Article', 3 => 'Third Article', 2 => 'Second Article'); $this->assertEquals($expected, $result); } $result = Hash::combine($TestModel->find('all', array('order' => 'Article.title ASC', 'fields' => array('id', 'title'))), '{n}.Article.id', '{n}.Article.title'); $expected = array(1 => 'First Article', 2 => 'Second Article', 3 => 'Third Article'); $this->assertEquals($expected, $result); $result = Hash::combine($TestModel->find('all', array('order' => 'Article.title ASC')), '{n}.Article.id', '{n}.Article'); $expected = array(1 => 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'), 2 => 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'), 3 => 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); $result = Hash::combine($TestModel->find('all', array('order' => 'Article.title ASC')), '{n}.Article.id', '{n}.Article', '{n}.Article.user_id'); $expected = array(1 => array(1 => 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'), 3 => 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')), 3 => array(2 => 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'))); $this->assertEquals($expected, $result); $result = Hash::combine($TestModel->find('all', array('order' => 'Article.title ASC', 'fields' => array('id', 'title', 'user_id'))), '{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id'); $expected = array(1 => array(1 => 'First Article', 3 => 'Third Article'), 3 => array(2 => 'Second Article')); $this->assertEquals($expected, $result); $TestModel = new Apple(); $expected = array(1 => 'Red Apple 1', 2 => 'Bright Red Apple', 3 => 'green blue', 4 => 'Test Name', 5 => 'Blue Green', 6 => 'My new apple', 7 => 'Some odd color'); $this->assertEquals($expected, $TestModel->find('list')); $this->assertEquals($expected, $TestModel->Parent->find('list')); $TestModel = new Post(); $result = $TestModel->find('list', array('fields' => 'Post.title')); $expected = array(1 => 'First Post', 2 => 'Second Post', 3 => 'Third Post'); $this->assertEquals($expected, $result); $result = $TestModel->find('list', array('fields' => 'title')); $expected = array(1 => 'First Post', 2 => 'Second Post', 3 => 'Third Post'); $this->assertEquals($expected, $result); $result = $TestModel->find('list', array('fields' => array('title', 'id'))); $expected = array('First Post' => '1', 'Second Post' => '2', 'Third Post' => '3'); $this->assertEquals($expected, $result); $result = $TestModel->find('list', array('fields' => array('title', 'id', 'created'))); $expected = array('2007-03-18 10:39:23' => array('First Post' => '1'), '2007-03-18 10:41:23' => array('Second Post' => '2'), '2007-03-18 10:43:23' => array('Third Post' => '3')); $this->assertEquals($expected, $result); $result = $TestModel->find('list', array('fields' => array('Post.body'))); $expected = array(1 => 'First Post Body', 2 => 'Second Post Body', 3 => 'Third Post Body'); $this->assertEquals($expected, $result); $result = $TestModel->find('list', array('fields' => array('Post.title', 'Post.body'))); $expected = array('First Post' => 'First Post Body', 'Second Post' => 'Second Post Body', 'Third Post' => 'Third Post Body'); $this->assertEquals($expected, $result); $result = $TestModel->find('list', array('fields' => array('Post.id', 'Post.title', 'Author.user'), 'recursive' => 1)); $expected = array('mariano' => array(1 => 'First Post', 3 => 'Third Post'), 'larry' => array(2 => 'Second Post')); $this->assertEquals($expected, $result); $TestModel = new User(); $result = $TestModel->find('list', array('fields' => array('User.user', 'User.password'))); $expected = array('mariano' => '5f4dcc3b5aa765d61d8327deb882cf99', 'nate' => '5f4dcc3b5aa765d61d8327deb882cf99', 'larry' => '5f4dcc3b5aa765d61d8327deb882cf99', 'garrett' => '5f4dcc3b5aa765d61d8327deb882cf99'); $this->assertEquals($expected, $result); $TestModel = new ModifiedAuthor(); $result = $TestModel->find('list', array('fields' => array('Author.id', 'Author.user'))); $expected = array(1 => 'mariano (CakePHP)', 2 => 'nate (CakePHP)', 3 => 'larry (CakePHP)', 4 => 'garrett (CakePHP)'); $this->assertEquals($expected, $result); $TestModel = new Article(); $TestModel->displayField = 'title'; $result = $TestModel->find('list', array('conditions' => array('User.user' => 'mariano'), 'recursive' => 0)); $expected = array(1 => 'First Article', 3 => 'Third Article'); $this->assertEquals($expected, $result); }
/** * testBehaviorBelongsToFindCallbacks method * * @access public * @return void */ function testBehaviorBelongsToFindCallbacks() { $Apple = new Apple(); $Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false); $expected = $Apple->find('all'); $Apple->unbindModel(array('belongsTo' => array('Parent'))); $wellBehaved = $Apple->find('all'); $Apple->Parent->Behaviors->attach('Test'); $Apple->unbindModel(array('belongsTo' => array('Parent'))); $this->assertIdentical($Apple->find('all'), $wellBehaved); $Apple->Parent->Behaviors->attach('Test', array('before' => 'off')); $this->assertIdentical($Apple->find('all'), $expected); $Apple->Parent->Behaviors->attach('Test', array('before' => 'test')); $this->assertIdentical($Apple->find('all'), $expected); $Apple->Parent->Behaviors->attach('Test', array('before' => 'modify')); $expected2 = array(array('Apple' => array('id' => 1), 'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')), array('Apple' => array('id' => 2), 'Parent' => array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17')), array('Apple' => array('id' => 3), 'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))); $result = $Apple->find('all', array('fields' => array('Apple.id', 'Parent.*'), 'conditions' => array('Apple.id <' => '4'))); //$this->assertEqual($result, $expected2); $Apple->Parent->Behaviors->disable('Test'); $result = $Apple->find('all'); $this->assertEqual($result, $expected); $Apple->Parent->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on')); //$this->assertIdentical($Apple->find('all'), array()); $Apple->Parent->Behaviors->attach('Test', array('after' => 'off')); $this->assertEqual($Apple->find('all'), $expected); $Apple->Parent->Behaviors->attach('Test', array('after' => 'test')); $this->assertEqual($Apple->find('all'), $expected); $Apple->Parent->Behaviors->attach('Test', array('after' => 'test2')); $this->assertEqual($Apple->find('all'), $expected); $Apple->Parent->Behaviors->attach('Test', array('after' => 'modify')); $expected = array(array('id' => '1', 'apple_id' => '2', 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'), array('id' => '2', 'apple_id' => '1', 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'), array('id' => '3', 'apple_id' => '2', 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'), array('id' => '4', 'apple_id' => '2', 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'), array('id' => '5', 'apple_id' => '5', 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'), array('id' => '6', 'apple_id' => '4', 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'), array('id' => '7', 'apple_id' => '6', 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17')); //$this->assertEqual($Apple->find('all'), $expected); }
/** * testBehaviorBelongsToFindCallbacks method * * @return void */ public function testBehaviorBelongsToFindCallbacks() { $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.'); $conditions = array('order' => 'Apple.id ASC'); $Apple = new Apple(); $Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false); $expected = $Apple->find('all', $conditions); $Apple->unbindModel(array('belongsTo' => array('Parent'))); $wellBehaved = $Apple->find('all', $conditions); $Apple->Parent->Behaviors->attach('Test'); $Apple->unbindModel(array('belongsTo' => array('Parent'))); $this->assertSame($Apple->find('all', $conditions), $wellBehaved); $Apple->Parent->Behaviors->attach('Test', array('before' => 'off')); $this->assertSame($expected, $Apple->find('all', $conditions)); $Apple->Parent->Behaviors->attach('Test', array('before' => 'test')); $this->assertSame($expected, $Apple->find('all', $conditions)); $Apple->Parent->Behaviors->attach('Test', array('before' => 'modify')); $expected2 = array(array('Apple' => array('id' => 1), 'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')), array('Apple' => array('id' => 2), 'Parent' => array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17')), array('Apple' => array('id' => 3), 'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))); $result2 = $Apple->find('all', array('fields' => array('Apple.id', 'Parent.id', 'Parent.name', 'Parent.mytime'), 'conditions' => array('Apple.id <' => '4'), 'order' => 'Apple.id ASC')); $this->assertEquals($expected2, $result2); $Apple->Parent->Behaviors->disable('Test'); $result = $Apple->find('all', $conditions); $this->assertEquals($expected, $result); $Apple->Parent->Behaviors->attach('Test', array('after' => 'off')); $this->assertEquals($expected, $Apple->find('all', $conditions)); $Apple->Parent->Behaviors->attach('Test', array('after' => 'test')); $this->assertEquals($expected, $Apple->find('all', $conditions)); $Apple->Parent->Behaviors->attach('Test', array('after' => 'test2')); $this->assertEquals($expected, $Apple->find('all', $conditions)); }
public function remove($appleID = null) { // Confirm and Delete an Tofu $this->apple = Apple::find($appleID); $this->apple->remove($_POST['apple']); }