PutTagTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Shaarli\Api\Controllers;
  3. use Shaarli\Api\Exceptions\ApiBadParametersException;
  4. use Shaarli\Config\ConfigManager;
  5. use Slim\Container;
  6. use Slim\Http\Environment;
  7. use Slim\Http\Request;
  8. use Slim\Http\Response;
  9. class PutTagTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var string datastore to test write operations
  13. */
  14. protected static $testDatastore = 'sandbox/datastore.php';
  15. /**
  16. * @var string datastore to test write operations
  17. */
  18. protected static $testHistory = 'sandbox/history.php';
  19. /**
  20. * @var ConfigManager instance
  21. */
  22. protected $conf;
  23. /**
  24. * @var \ReferenceLinkDB instance.
  25. */
  26. protected $refDB = null;
  27. /**
  28. * @var \History instance.
  29. */
  30. protected $history;
  31. /**
  32. * @var Container instance.
  33. */
  34. protected $container;
  35. /**
  36. * @var \LinkDB instance.
  37. */
  38. protected $linkDB;
  39. /**
  40. * @var Tags controller instance.
  41. */
  42. protected $controller;
  43. /**
  44. * Number of JSON field per link.
  45. */
  46. const NB_FIELDS_TAG = 2;
  47. /**
  48. * Before every test, instantiate a new Api with its config, plugins and links.
  49. */
  50. public function setUp()
  51. {
  52. $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
  53. $this->refDB = new \ReferenceLinkDB();
  54. $this->refDB->write(self::$testDatastore);
  55. $refHistory = new \ReferenceHistory();
  56. $refHistory->write(self::$testHistory);
  57. $this->history = new \History(self::$testHistory);
  58. $this->container = new Container();
  59. $this->container['conf'] = $this->conf;
  60. $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
  61. $this->container['db'] = $this->linkDB;
  62. $this->container['history'] = $this->history;
  63. $this->controller = new Tags($this->container);
  64. }
  65. /**
  66. * After every test, remove the test datastore.
  67. */
  68. public function tearDown()
  69. {
  70. @unlink(self::$testDatastore);
  71. @unlink(self::$testHistory);
  72. }
  73. /**
  74. * Test tags update
  75. */
  76. public function testPutLinkValid()
  77. {
  78. $env = Environment::mock([
  79. 'REQUEST_METHOD' => 'PUT',
  80. ]);
  81. $tagName = 'gnu';
  82. $update = ['name' => $newName = 'newtag'];
  83. $request = Request::createFromEnvironment($env);
  84. $request = $request->withParsedBody($update);
  85. $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
  86. $this->assertEquals(200, $response->getStatusCode());
  87. $data = json_decode((string) $response->getBody(), true);
  88. $this->assertEquals(self::NB_FIELDS_TAG, count($data));
  89. $this->assertEquals($newName, $data['name']);
  90. $this->assertEquals(2, $data['occurrences']);
  91. $tags = $this->linkDB->linksCountPerTag();
  92. $this->assertNotTrue(isset($tags[$tagName]));
  93. $this->assertEquals(2, $tags[$newName]);
  94. $historyEntry = $this->history->getHistory()[0];
  95. $this->assertEquals(\History::UPDATED, $historyEntry['event']);
  96. $this->assertTrue(
  97. (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
  98. );
  99. $historyEntry = $this->history->getHistory()[1];
  100. $this->assertEquals(\History::UPDATED, $historyEntry['event']);
  101. $this->assertTrue(
  102. (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
  103. );
  104. }
  105. /**
  106. * Test tag update with an existing tag: they should be merged
  107. */
  108. public function testPutTagMerge()
  109. {
  110. $tagName = 'gnu';
  111. $newName = 'w3c';
  112. $tags = $this->linkDB->linksCountPerTag();
  113. $this->assertEquals(1, $tags[$newName]);
  114. $this->assertEquals(2, $tags[$tagName]);
  115. $env = Environment::mock([
  116. 'REQUEST_METHOD' => 'PUT',
  117. ]);
  118. $update = ['name' => $newName];
  119. $request = Request::createFromEnvironment($env);
  120. $request = $request->withParsedBody($update);
  121. $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
  122. $this->assertEquals(200, $response->getStatusCode());
  123. $data = json_decode((string) $response->getBody(), true);
  124. $this->assertEquals(self::NB_FIELDS_TAG, count($data));
  125. $this->assertEquals($newName, $data['name']);
  126. $this->assertEquals(3, $data['occurrences']);
  127. $tags = $this->linkDB->linksCountPerTag();
  128. $this->assertNotTrue(isset($tags[$tagName]));
  129. $this->assertEquals(3, $tags[$newName]);
  130. }
  131. /**
  132. * Test tag update with an empty new tag name => ApiBadParametersException
  133. *
  134. * @expectedException Shaarli\Api\Exceptions\ApiBadParametersException
  135. * @expectedExceptionMessage New tag name is required in the request body
  136. */
  137. public function testPutTagEmpty()
  138. {
  139. $tagName = 'gnu';
  140. $newName = '';
  141. $tags = $this->linkDB->linksCountPerTag();
  142. $this->assertEquals(2, $tags[$tagName]);
  143. $env = Environment::mock([
  144. 'REQUEST_METHOD' => 'PUT',
  145. ]);
  146. $request = Request::createFromEnvironment($env);
  147. $env = Environment::mock([
  148. 'REQUEST_METHOD' => 'PUT',
  149. ]);
  150. $update = ['name' => $newName];
  151. $request = Request::createFromEnvironment($env);
  152. $request = $request->withParsedBody($update);
  153. try {
  154. $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
  155. } catch (ApiBadParametersException $e) {
  156. $tags = $this->linkDB->linksCountPerTag();
  157. $this->assertEquals(2, $tags[$tagName]);
  158. throw $e;
  159. }
  160. }
  161. /**
  162. * Test tag update on non existent tag => ApiTagNotFoundException.
  163. *
  164. * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
  165. * @expectedExceptionMessage Tag not found
  166. */
  167. public function testPutTag404()
  168. {
  169. $env = Environment::mock([
  170. 'REQUEST_METHOD' => 'PUT',
  171. ]);
  172. $request = Request::createFromEnvironment($env);
  173. $this->controller->putTag($request, new Response(), ['tagName' => 'nopenope']);
  174. }
  175. }