ResetBehaviorTest.php 5.1 KB

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