ResetBehaviorTest.php 5.7 KB

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