ResetBehaviorTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 = ['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', ['order' => ['updated' => 'DESC']]);
  21. $result = $this->Model->resetRecords();
  22. $this->assertTrue((bool)$result);
  23. $y = $this->Model->find('first', ['order' => ['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', ['updateTimestamp' => true]);
  29. $x = $this->Model->find('first', ['order' => ['updated' => 'DESC']]);
  30. $this->assertTrue($x['MyComment']['updated'] < '2007-12-31');
  31. $result = $this->Model->resetRecords();
  32. $this->assertTrue((bool)$result);
  33. $x = $this->Model->find('first', ['order' => ['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', ['callback' => 'customCallback']);
  39. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  40. $this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
  41. $result = $this->Model->resetRecords();
  42. $this->assertTrue((bool)$result);
  43. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  44. $expected = 'Second Comment for Second Article xyz';
  45. $this->assertEquals($expected, $x['MyComment']['comment']);
  46. }
  47. public function testResetWithObjectCallback() {
  48. $this->Model->Behaviors->unload('Reset');
  49. $this->Model->Behaviors->load('Tools.Reset', ['callback' => [$this->Model, 'customObjectCallback']]);
  50. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  51. $this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
  52. $result = $this->Model->resetRecords();
  53. $this->assertTrue((bool)$result);
  54. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  55. $expected = 'Second Comment for Second Article xxx';
  56. $this->assertEquals($expected, $x['MyComment']['comment']);
  57. }
  58. public function testResetWithStaticCallback() {
  59. $this->Model->Behaviors->unload('Reset');
  60. $this->Model->Behaviors->load('Tools.Reset', ['callback' => 'MyComment::customStaticCallback']);
  61. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  62. $this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
  63. $result = $this->Model->resetRecords();
  64. $this->assertTrue((bool)$result);
  65. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  66. $expected = 'Second Comment for Second Article yyy';
  67. $this->assertEquals($expected, $x['MyComment']['comment']);
  68. }
  69. public function testResetWithCallbackAndFields() {
  70. $this->Model->Behaviors->unload('Reset');
  71. $this->Model->Behaviors->load('Tools.Reset', [
  72. 'fields' => ['id'],
  73. 'updateFields' => ['comment'],
  74. 'callback' => 'MyComment::fieldsCallback']);
  75. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  76. $this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
  77. $result = $this->Model->resetRecords();
  78. $this->assertTrue((bool)$result);
  79. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  80. $expected = 'foo';
  81. $this->assertEquals($expected, $x['MyComment']['comment']);
  82. }
  83. public function testResetWithCallbackAndFieldsAutoAdded() {
  84. $this->Model->Behaviors->unload('Reset');
  85. $this->Model->Behaviors->load('Tools.Reset', [
  86. 'fields' => ['id'],
  87. 'updateFields' => ['id'],
  88. 'callback' => 'MyComment::fieldsCallbackAuto']);
  89. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  90. $this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
  91. $result = $this->Model->resetRecords();
  92. $this->assertTrue((bool)$result);
  93. $x = $this->Model->find('first', ['conditions' => ['id' => 6]]);
  94. $expected = 'bar';
  95. $this->assertEquals($expected, $x['MyComment']['comment']);
  96. }
  97. }
  98. class MyComment extends AppModel {
  99. public $fixture = 'core.comment';
  100. public $useTable = 'comments';
  101. public $displayField = 'comment';
  102. public function customCallback($data, &$updateFields) {
  103. $data[$this->alias][$this->displayField] .= ' xyz';
  104. $fields[] = 'some_other_field';
  105. return $data;
  106. }
  107. public function customObjectCallback($data, &$updateFields) {
  108. $data[$this->alias][$this->displayField] .= ' xxx';
  109. $updateFields[] = 'some_other_field';
  110. return $data;
  111. }
  112. public static function customStaticCallback($data, &$updateFields) {
  113. $data['MyComment']['comment'] .= ' yyy';
  114. $updateFields[] = 'some_other_field';
  115. return $data;
  116. }
  117. public static function fieldsCallback($data, &$updateFields) {
  118. $data['MyComment']['comment'] = 'foo';
  119. return $data;
  120. }
  121. public static function fieldsCallbackAuto($data, &$updateFields) {
  122. $data['MyComment']['comment'] = 'bar';
  123. $updateFields[] = 'comment';
  124. return $data;
  125. }
  126. }