ResetBehaviorTest.php 5.7 KB

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