ResetBehaviorTest.php 5.7 KB

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