ResetBehaviorTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Tools\Model\Behavior;
  3. use Cake\ORM\TableRegistry;
  4. use Tools\Model\Table\Table;
  5. use Tools\TestSuite\TestCase;
  6. class ResetBehaviorTest extends TestCase {
  7. /**
  8. * @var \Tools\Model\Behavior\ResetBehavior
  9. */
  10. public $ResetBehavior;
  11. /**
  12. * @var \Tools\Model\Table\Table
  13. */
  14. public $Table;
  15. /**
  16. * @var array
  17. */
  18. public $fixtures = [
  19. 'plugin.Tools.ResetComments'
  20. ];
  21. /**
  22. * @return void
  23. */
  24. public function setUp() {
  25. parent::setUp();
  26. $this->Table = TableRegistry::getTableLocator()->get('ResetComments');
  27. $this->Table->addBehavior('Tools.Reset');
  28. }
  29. /**
  30. * @return void
  31. */
  32. public function tearDown() {
  33. TableRegistry::clear();
  34. parent::tearDown();
  35. }
  36. /**
  37. * @return void
  38. */
  39. public function testResetRecords() {
  40. $x = $this->Table->find('all', ['fields' => ['comment', 'updated'], 'order' => ['updated' => 'DESC']])->first();
  41. $x['updated'] = (string)$x['updated'];
  42. $result = $this->Table->resetRecords();
  43. $this->assertTrue((bool)$result);
  44. $y = $this->Table->find('all', ['fields' => ['comment', 'updated'], 'order' => ['updated' => 'DESC']])->first();
  45. $y['updated'] = (string)$y['updated'];
  46. $this->assertSame($x->toArray(), $y->toArray());
  47. }
  48. /**
  49. * @return void
  50. */
  51. public function testResetRecordsUpdateField() {
  52. $this->Table->removeBehavior('Reset');
  53. $this->Table->addBehavior('Tools.Reset', ['fields' => ['comment'], 'updateFields' => ['comment']]);
  54. $x = $this->Table->find('all', ['fields' => ['comment'], 'order' => ['updated' => 'DESC']])->first();
  55. $result = $this->Table->resetRecords();
  56. $this->assertTrue((bool)$result);
  57. $y = $this->Table->find('all', ['fields' => ['comment'], 'order' => ['updated' => 'DESC']])->first();
  58. $this->assertSame($x->toArray(), $y->toArray());
  59. }
  60. /**
  61. * ResetBehaviorTest::testResetRecordsWithUpdatedTimestamp()
  62. *
  63. * @return void
  64. */
  65. public function _testResetRecordsWithUpdatedTimestamp() {
  66. $this->Table->removeBehavior('Reset');
  67. $this->Table->addBehavior('Tools.Reset', ['updateTimestamp' => true]);
  68. $x = $this->Table->find('all', ['order' => ['updated' => 'DESC']])->first();
  69. $this->assertTrue($x['updated'] < '2007-12-31');
  70. $result = $this->Table->resetRecords();
  71. $this->assertTrue((bool)$result);
  72. $x = $this->Table->find('all', ['order' => ['updated' => 'ASC']])->first();
  73. $this->assertTrue($x['updated'] > (date('Y') - 1) . '-12-31');
  74. }
  75. /**
  76. * ResetBehaviorTest::testResetWithCallback()
  77. *
  78. * @return void
  79. */
  80. public function testResetWithCallback() {
  81. $this->Table->removeBehavior('Reset');
  82. $this->Table->addBehavior('Tools.Reset', ['callback' => 'customCallback']);
  83. $x = $this->Table->find('all', ['conditions' => ['id' => 6]])->first();
  84. $this->assertEquals('Second Comment for Second Article', $x['comment']);
  85. $result = $this->Table->resetRecords();
  86. $this->assertTrue((bool)$result);
  87. $x = $this->Table->find('all', ['conditions' => ['id' => 6]])->first();
  88. $expected = 'Second Comment for Second Article xyz';
  89. $this->assertEquals($expected, $x['comment']);
  90. }
  91. /**
  92. * ResetBehaviorTest::testResetWithObjectCallback()
  93. *
  94. * @return void
  95. */
  96. public function testResetWithObjectCallback() {
  97. $this->Table->removeBehavior('Reset');
  98. $this->Table->addBehavior('Tools.Reset', ['callback' => [$this->Table, 'customObjectCallback']]);
  99. $x = $this->Table->find('first', ['conditions' => ['id' => 6]]);
  100. $this->assertEquals('Second Comment for Second Article', $x['comment']);
  101. $result = $this->Table->resetRecords();
  102. $this->assertTrue((bool)$result);
  103. $x = $this->Table->find('first', ['conditions' => ['id' => 6]]);
  104. $expected = 'Second Comment for Second Article xxx';
  105. $this->assertEquals($expected, $x['comment']);
  106. }
  107. /**
  108. * ResetBehaviorTest::testResetWithStaticCallback()
  109. *
  110. * @return void
  111. */
  112. public function testResetWithStaticCallback() {
  113. $this->Table->removeBehavior('Reset');
  114. $this->Table->addBehavior('Tools.Reset', ['callback' => 'App\Model\Table\ResetCommentsTable::customStaticCallback']);
  115. $x = $this->Table->find('first', ['conditions' => ['id' => 6]]);
  116. $this->assertEquals('Second Comment for Second Article', $x['comment']);
  117. $result = $this->Table->resetRecords();
  118. $this->assertTrue((bool)$result);
  119. $x = $this->Table->find('first', ['conditions' => ['id' => 6]]);
  120. $expected = 'Second Comment for Second Article yyy';
  121. $this->assertEquals($expected, $x['comment']);
  122. }
  123. /**
  124. * ResetBehaviorTest::testResetWithCallbackAndFields()
  125. *
  126. * @return void
  127. */
  128. public function testResetWithCallbackAndFields() {
  129. $this->Table->removeBehavior('Reset');
  130. $this->Table->addBehavior('Tools.Reset', [
  131. 'fields' => ['id'],
  132. 'updateFields' => ['comment'],
  133. 'callback' => 'App\Model\Table\ResetCommentsTable::fieldsCallback']);
  134. $x = $this->Table->find('first', ['conditions' => ['id' => 6]]);
  135. $this->assertEquals('Second Comment for Second Article', $x['comment']);
  136. $result = $this->Table->resetRecords();
  137. $this->assertTrue((bool)$result);
  138. $x = $this->Table->find('first', ['conditions' => ['id' => 6]]);
  139. $expected = 'foo';
  140. $this->assertEquals($expected, $x['comment']);
  141. }
  142. /**
  143. * ResetBehaviorTest::testResetWithCallbackAndFieldsAutoAdded()
  144. *
  145. * @return void
  146. */
  147. public function testResetWithCallbackAndFieldsAutoAdded() {
  148. $this->Table->removeBehavior('Reset');
  149. $this->Table->addBehavior('Tools.Reset', [
  150. 'fields' => ['id'],
  151. 'updateFields' => ['id'],
  152. 'callback' => 'App\Model\Table\ResetCommentsTable::fieldsCallbackAuto']);
  153. $x = $this->Table->find('first', ['conditions' => ['id' => 6]]);
  154. $this->assertEquals('Second Comment for Second Article', $x['comment']);
  155. $result = $this->Table->resetRecords();
  156. $this->assertTrue((bool)$result);
  157. $x = $this->Table->find('first', ['conditions' => ['id' => 6]]);
  158. $expected = 'bar';
  159. $this->assertEquals($expected, $x['comment']);
  160. }
  161. }