ResetBehaviorTest.php 5.7 KB

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