DeleteTagTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Shaarli\Api\Controllers;
  3. use Shaarli\Config\ConfigManager;
  4. use Slim\Container;
  5. use Slim\Http\Environment;
  6. use Slim\Http\Request;
  7. use Slim\Http\Response;
  8. class DeleteTagTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var string datastore to test write operations
  12. */
  13. protected static $testDatastore = 'sandbox/datastore.php';
  14. /**
  15. * @var string datastore to test write operations
  16. */
  17. protected static $testHistory = 'sandbox/history.php';
  18. /**
  19. * @var ConfigManager instance
  20. */
  21. protected $conf;
  22. /**
  23. * @var \ReferenceLinkDB instance.
  24. */
  25. protected $refDB = null;
  26. /**
  27. * @var \LinkDB instance.
  28. */
  29. protected $linkDB;
  30. /**
  31. * @var \History instance.
  32. */
  33. protected $history;
  34. /**
  35. * @var Container instance.
  36. */
  37. protected $container;
  38. /**
  39. * @var Tags controller instance.
  40. */
  41. protected $controller;
  42. /**
  43. * Before each test, instantiate a new Api with its config, plugins and links.
  44. */
  45. public function setUp()
  46. {
  47. $this->conf = new ConfigManager('tests/utils/config/configJson');
  48. $this->refDB = new \ReferenceLinkDB();
  49. $this->refDB->write(self::$testDatastore);
  50. $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
  51. $refHistory = new \ReferenceHistory();
  52. $refHistory->write(self::$testHistory);
  53. $this->history = new \History(self::$testHistory);
  54. $this->container = new Container();
  55. $this->container['conf'] = $this->conf;
  56. $this->container['db'] = $this->linkDB;
  57. $this->container['history'] = $this->history;
  58. $this->controller = new Tags($this->container);
  59. }
  60. /**
  61. * After each test, remove the test datastore.
  62. */
  63. public function tearDown()
  64. {
  65. @unlink(self::$testDatastore);
  66. @unlink(self::$testHistory);
  67. }
  68. /**
  69. * Test DELETE tag endpoint: the tag should be removed.
  70. */
  71. public function testDeleteTagValid()
  72. {
  73. $tagName = 'gnu';
  74. $tags = $this->linkDB->linksCountPerTag();
  75. $this->assertTrue($tags[$tagName] > 0);
  76. $env = Environment::mock([
  77. 'REQUEST_METHOD' => 'DELETE',
  78. ]);
  79. $request = Request::createFromEnvironment($env);
  80. $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
  81. $this->assertEquals(204, $response->getStatusCode());
  82. $this->assertEmpty((string) $response->getBody());
  83. $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
  84. $tags = $this->linkDB->linksCountPerTag();
  85. $this->assertFalse(isset($tags[$tagName]));
  86. // 2 links affected
  87. $historyEntry = $this->history->getHistory()[0];
  88. $this->assertEquals(\History::UPDATED, $historyEntry['event']);
  89. $this->assertTrue(
  90. (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
  91. );
  92. $historyEntry = $this->history->getHistory()[1];
  93. $this->assertEquals(\History::UPDATED, $historyEntry['event']);
  94. $this->assertTrue(
  95. (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
  96. );
  97. }
  98. /**
  99. * Test DELETE tag endpoint: the tag should be removed.
  100. */
  101. public function testDeleteTagCaseSensitivity()
  102. {
  103. $tagName = 'sTuff';
  104. $tags = $this->linkDB->linksCountPerTag();
  105. $this->assertTrue($tags[$tagName] > 0);
  106. $env = Environment::mock([
  107. 'REQUEST_METHOD' => 'DELETE',
  108. ]);
  109. $request = Request::createFromEnvironment($env);
  110. $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
  111. $this->assertEquals(204, $response->getStatusCode());
  112. $this->assertEmpty((string) $response->getBody());
  113. $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
  114. $tags = $this->linkDB->linksCountPerTag();
  115. $this->assertFalse(isset($tags[$tagName]));
  116. $this->assertTrue($tags[strtolower($tagName)] > 0);
  117. $historyEntry = $this->history->getHistory()[0];
  118. $this->assertEquals(\History::UPDATED, $historyEntry['event']);
  119. $this->assertTrue(
  120. (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
  121. );
  122. }
  123. /**
  124. * Test DELETE tag endpoint: reach not existing tag.
  125. *
  126. * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
  127. * @expectedExceptionMessage Tag not found
  128. */
  129. public function testDeleteLink404()
  130. {
  131. $tagName = 'nopenope';
  132. $tags = $this->linkDB->linksCountPerTag();
  133. $this->assertFalse(isset($tags[$tagName]));
  134. $env = Environment::mock([
  135. 'REQUEST_METHOD' => 'DELETE',
  136. ]);
  137. $request = Request::createFromEnvironment($env);
  138. $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
  139. }
  140. }