ResetBehaviorTest.php 5.3 KB

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