HistoryTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. require_once 'application/History.php';
  3. class HistoryTest extends PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. * @var string History file path
  7. */
  8. protected static $historyFilePath = 'sandbox/history.php';
  9. /**
  10. * Delete history file.
  11. */
  12. public function tearDown()
  13. {
  14. @unlink(self::$historyFilePath);
  15. }
  16. /**
  17. * Test that the history file is created if it doesn't exist.
  18. */
  19. public function testConstructLazyLoading()
  20. {
  21. new History(self::$historyFilePath);
  22. $this->assertFileNotExists(self::$historyFilePath);
  23. }
  24. /**
  25. * Test that the history file is created if it doesn't exist.
  26. */
  27. public function testAddEventCreateFile()
  28. {
  29. $history = new History(self::$historyFilePath);
  30. $history->updateSettings();
  31. $this->assertFileExists(self::$historyFilePath);
  32. }
  33. /**
  34. * Not writable history file: raise an exception.
  35. *
  36. * @expectedException Exception
  37. * @expectedExceptionMessage History file isn't readable or writable
  38. */
  39. public function testConstructNotWritable()
  40. {
  41. touch(self::$historyFilePath);
  42. chmod(self::$historyFilePath, 0440);
  43. $history = new History(self::$historyFilePath);
  44. $history->updateSettings();
  45. }
  46. /**
  47. * Not parsable history file: raise an exception.
  48. *
  49. * @expectedException Exception
  50. * @expectedExceptionMessageRegExp /Could not parse history file/
  51. */
  52. public function testConstructNotParsable()
  53. {
  54. file_put_contents(self::$historyFilePath, 'not parsable');
  55. $history = new History(self::$historyFilePath);
  56. // gzinflate generates a warning
  57. @$history->updateSettings();
  58. }
  59. /**
  60. * Test add link event
  61. */
  62. public function testAddLink()
  63. {
  64. $history = new History(self::$historyFilePath);
  65. $history->addLink(['id' => 0]);
  66. $actual = $history->getHistory()[0];
  67. $this->assertEquals(History::CREATED, $actual['event']);
  68. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  69. $this->assertEquals(0, $actual['id']);
  70. $history = new History(self::$historyFilePath);
  71. $history->addLink(['id' => 1]);
  72. $actual = $history->getHistory()[0];
  73. $this->assertEquals(History::CREATED, $actual['event']);
  74. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  75. $this->assertEquals(1, $actual['id']);
  76. $history = new History(self::$historyFilePath);
  77. $history->addLink(['id' => 'str']);
  78. $actual = $history->getHistory()[0];
  79. $this->assertEquals(History::CREATED, $actual['event']);
  80. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  81. $this->assertEquals('str', $actual['id']);
  82. }
  83. /**
  84. * Test updated link event
  85. */
  86. public function testUpdateLink()
  87. {
  88. $history = new History(self::$historyFilePath);
  89. $history->updateLink(['id' => 1]);
  90. $actual = $history->getHistory()[0];
  91. $this->assertEquals(History::UPDATED, $actual['event']);
  92. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  93. $this->assertEquals(1, $actual['id']);
  94. }
  95. /**
  96. * Test delete link event
  97. */
  98. public function testDeleteLink()
  99. {
  100. $history = new History(self::$historyFilePath);
  101. $history->deleteLink(['id' => 1]);
  102. $actual = $history->getHistory()[0];
  103. $this->assertEquals(History::DELETED, $actual['event']);
  104. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  105. $this->assertEquals(1, $actual['id']);
  106. }
  107. /**
  108. * Test updated settings event
  109. */
  110. public function testUpdateSettings()
  111. {
  112. $history = new History(self::$historyFilePath);
  113. $history->updateSettings();
  114. $actual = $history->getHistory()[0];
  115. $this->assertEquals(History::SETTINGS, $actual['event']);
  116. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  117. $this->assertEmpty($actual['id']);
  118. }
  119. /**
  120. * Make sure that new items are stored at the beginning
  121. */
  122. public function testHistoryOrder()
  123. {
  124. $history = new History(self::$historyFilePath);
  125. $history->updateLink(['id' => 1]);
  126. $actual = $history->getHistory()[0];
  127. $this->assertEquals(History::UPDATED, $actual['event']);
  128. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  129. $this->assertEquals(1, $actual['id']);
  130. $history->addLink(['id' => 1]);
  131. $actual = $history->getHistory()[0];
  132. $this->assertEquals(History::CREATED, $actual['event']);
  133. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  134. $this->assertEquals(1, $actual['id']);
  135. }
  136. /**
  137. * Re-read history from file after writing an event
  138. */
  139. public function testHistoryRead()
  140. {
  141. $history = new History(self::$historyFilePath);
  142. $history->updateLink(['id' => 1]);
  143. $history = new History(self::$historyFilePath);
  144. $actual = $history->getHistory()[0];
  145. $this->assertEquals(History::UPDATED, $actual['event']);
  146. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  147. $this->assertEquals(1, $actual['id']);
  148. }
  149. /**
  150. * Re-read history from file after writing an event and make sure that the order is correct
  151. */
  152. public function testHistoryOrderRead()
  153. {
  154. $history = new History(self::$historyFilePath);
  155. $history->updateLink(['id' => 1]);
  156. $history->addLink(['id' => 1]);
  157. $history = new History(self::$historyFilePath);
  158. $actual = $history->getHistory()[0];
  159. $this->assertEquals(History::CREATED, $actual['event']);
  160. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  161. $this->assertEquals(1, $actual['id']);
  162. $actual = $history->getHistory()[1];
  163. $this->assertEquals(History::UPDATED, $actual['event']);
  164. $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
  165. $this->assertEquals(1, $actual['id']);
  166. }
  167. /**
  168. * Test retention time: delete old entries.
  169. */
  170. public function testHistoryRententionTime()
  171. {
  172. $history = new History(self::$historyFilePath, 5);
  173. $history->updateLink(['id' => 1]);
  174. $this->assertEquals(1, count($history->getHistory()));
  175. $arr = $history->getHistory();
  176. $arr[0]['datetime'] = new DateTime('-1 hour');
  177. FileUtils::writeFlatDB(self::$historyFilePath, $arr);
  178. $history = new History(self::$historyFilePath, 60);
  179. $this->assertEquals(1, count($history->getHistory()));
  180. $this->assertEquals(1, $history->getHistory()[0]['id']);
  181. $history->updateLink(['id' => 2]);
  182. $this->assertEquals(1, count($history->getHistory()));
  183. $this->assertEquals(2, $history->getHistory()[0]['id']);
  184. }
  185. }