ResetBehaviorTest.php 5.2 KB

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