MultipleDisplayFieldsBehaviorTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. App::uses('MultipleDisplayFieldsBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class MultipleDisplayFieldsBehaviorTest extends MyCakeTestCase {
  5. public $fixtures = array('core.comment', 'core.user');
  6. public $Comment;
  7. public $MultipleDisplayFieldsBehavior;
  8. public function setUp() {
  9. parent::setUp();
  10. $this->MultipleDisplayFieldsBehavior = new MultipleDisplayFieldsBehavior();
  11. $this->Comment = ClassRegistry::init('Comment');
  12. $this->Comment->bindModel(array('belongsTo' => array('User')), false);
  13. $this->Comment->displayField = 'comment';
  14. }
  15. public function testObject() {
  16. $this->assertTrue(is_object($this->MultipleDisplayFieldsBehavior));
  17. $this->assertInstanceOf('MultipleDisplayFieldsBehavior', $this->MultipleDisplayFieldsBehavior);
  18. }
  19. public function testSimple() {
  20. $this->Comment->Behaviors->load('Tools.MultipleDisplayFields');
  21. $res = $this->Comment->find('first');
  22. $this->assertSame(7, count($res['Comment']));
  23. $config = array(
  24. 'fields' => array(
  25. $this->Comment->alias . '.comment', $this->Comment->alias . '.published'
  26. ),
  27. 'pattern' => '%s (%s)',
  28. );
  29. $this->Comment->Behaviors->load('Tools.MultipleDisplayFields', $config);
  30. $res = $this->Comment->find('list');
  31. $this->debug($res);
  32. $this->assertEquals('First Comment for First Article (Y)', $res[1]);
  33. }
  34. public function testAdvanced() {
  35. //$res = $this->Comment->find('first', array('contain' => array('User')));
  36. $config = array(
  37. 'fields' => array(
  38. $this->Comment->alias . '.comment', $this->Comment->User->alias . '.user', $this->Comment->alias . '.published'
  39. ),
  40. 'displayField' => array('display_field'),
  41. 'pattern' => '%s by %s (%s)',
  42. );
  43. $this->Comment->Behaviors->load('Tools.MultipleDisplayFields', $config);
  44. $res = $this->Comment->find('list', array('contain' => array('User')));
  45. $this->debug($res);
  46. $this->assertEquals('First Comment for First Article by Y (nate)', $res[1]);
  47. }
  48. }