ResetBehaviorTest.php 5.2 KB

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