/**
  * 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);
 }
예제 #2
0
 /**
  * Tests that callbacks can be properly disabled
  *
  * @access public
  * @return void
  */
 function testCallbackDisabling()
 {
     $this->loadFixtures('Author');
     $TestModel = new ModifiedAuthor();
     $result = Set::extract($TestModel->find('all'), '/Author/user');
     $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
     $this->assertEqual($result, $expected);
     $result = Set::extract($TestModel->find('all', array('callbacks' => 'after')), '/Author/user');
     $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
     $this->assertEqual($result, $expected);
     $result = Set::extract($TestModel->find('all', array('callbacks' => 'before')), '/Author/user');
     $expected = array('mariano', 'nate', 'larry', 'garrett');
     $this->assertEqual($result, $expected);
     $result = Set::extract($TestModel->find('all', array('callbacks' => false)), '/Author/user');
     $expected = array('mariano', 'nate', 'larry', 'garrett');
     $this->assertEqual($result, $expected);
 }