ResetBehaviorTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. App::uses('ResetBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. App::uses('AppModel', 'Model');
  5. class ResetBehaviorTest extends MyCakeTestCase {
  6. public $ResetBehavior;
  7. public $Model;
  8. public $fixtures = array('core.comment');
  9. public function setUp() {
  10. parent::setUp();
  11. $this->ResetBehavior = new ResetBehavior();
  12. $this->Model = ClassRegistry::init('MyComment');
  13. $this->Model->Behaviors->load('Tools.Reset');
  14. }
  15. public function testObject() {
  16. $this->assertTrue(is_object($this->ResetBehavior));
  17. $this->assertInstanceOf('ResetBehavior', $this->ResetBehavior);
  18. }
  19. public function testResetRecords() {
  20. $x = $this->Model->find('first', array('order' => array('updated'=>'DESC')));
  21. $result = $this->Model->resetRecords();
  22. $this->assertTrue($result);
  23. $y = $this->Model->find('first', array('order' => array('updated'=>'DESC')));
  24. $this->assertSame($x, $y);
  25. }
  26. public function testResetRecordsWithUpdatedTimestamp() {
  27. $this->Model->Behaviors->unload('Reset');
  28. $this->Model->Behaviors->load('Tools.Reset', array('updateTimestamp' => true));
  29. $x = $this->Model->find('first', array('order' => array('updated'=>'DESC')));
  30. $this->assertTrue($x['MyComment']['updated'] < '2007-12-31');
  31. $result = $this->Model->resetRecords();
  32. $this->assertTrue($result);
  33. $x = $this->Model->find('first', array('order' => array('updated'=>'ASC')));
  34. $this->assertTrue($x['MyComment']['updated'] > (date('Y')-1) . '-12-31');
  35. }
  36. public function testResetWithCallback() {
  37. $this->Model->Behaviors->unload('Reset');
  38. $this->Model->Behaviors->load('Tools.Reset', array('callback' => 'customCallback'));
  39. $x = $this->Model->find('first', array('conditions' => array('id'=>6)));
  40. $this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
  41. $result = $this->Model->resetRecords();
  42. $this->assertTrue($result);
  43. $x = $this->Model->find('first', array('conditions' => array('id'=>6)));
  44. $expected = 'Second Comment for Second Article xyz';
  45. $this->assertEquals($expected, $x['MyComment']['comment']);
  46. }
  47. }
  48. class MyComment extends AppModel {
  49. public $fixture = 'core.comment';
  50. public $useTable = 'comments';
  51. public $displayField = 'comment';
  52. public function customCallback(&$data, &$fields) {
  53. $data[$this->alias][$this->displayField] .= ' xyz';
  54. $fields[] = 'some_other_field';
  55. }
  56. }