PutLinkTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 PutLinkTest 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 \History instance.
  28. */
  29. protected $history;
  30. /**
  31. * @var Container instance.
  32. */
  33. protected $container;
  34. /**
  35. * @var Links controller instance.
  36. */
  37. protected $controller;
  38. /**
  39. * Number of JSON field per link.
  40. */
  41. const NB_FIELDS_LINK = 9;
  42. /**
  43. * Before every 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.json.php');
  48. $this->refDB = new \ReferenceLinkDB();
  49. $this->refDB->write(self::$testDatastore);
  50. $refHistory = new \ReferenceHistory();
  51. $refHistory->write(self::$testHistory);
  52. $this->history = new \History(self::$testHistory);
  53. $this->container = new Container();
  54. $this->container['conf'] = $this->conf;
  55. $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
  56. $this->container['history'] = new \History(self::$testHistory);
  57. $this->controller = new Links($this->container);
  58. // Used by index_url().
  59. $this->controller->getCi()['environment'] = [
  60. 'SERVER_NAME' => 'domain.tld',
  61. 'SERVER_PORT' => 80,
  62. 'SCRIPT_NAME' => '/',
  63. ];
  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 link update without value: reset the link to default values
  75. */
  76. public function testPutLinkMinimal()
  77. {
  78. $env = Environment::mock([
  79. 'REQUEST_METHOD' => 'PUT',
  80. ]);
  81. $id = '41';
  82. $request = Request::createFromEnvironment($env);
  83. $response = $this->controller->putLink($request, new Response(), ['id' => $id]);
  84. $this->assertEquals(200, $response->getStatusCode());
  85. $data = json_decode((string) $response->getBody(), true);
  86. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  87. $this->assertEquals($id, $data['id']);
  88. $this->assertEquals('WDWyig', $data['shorturl']);
  89. $this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
  90. $this->assertEquals('?WDWyig', $data['title']);
  91. $this->assertEquals('', $data['description']);
  92. $this->assertEquals([], $data['tags']);
  93. $this->assertEquals(false, $data['private']);
  94. $this->assertEquals(
  95. \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
  96. \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
  97. );
  98. $this->assertTrue(
  99. new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
  100. );
  101. $historyEntry = $this->history->getHistory()[0];
  102. $this->assertEquals(\History::UPDATED, $historyEntry['event']);
  103. $this->assertTrue(
  104. (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
  105. );
  106. $this->assertEquals($id, $historyEntry['id']);
  107. }
  108. /**
  109. * Test link update with new values
  110. */
  111. public function testPutLinkWithValues()
  112. {
  113. $env = Environment::mock([
  114. 'REQUEST_METHOD' => 'PUT',
  115. 'CONTENT_TYPE' => 'application/json'
  116. ]);
  117. $id = 41;
  118. $update = [
  119. 'url' => 'http://somewhere.else',
  120. 'title' => 'Le Cid',
  121. 'description' => 'Percé jusques au fond du cœur [...]',
  122. 'tags' => ['corneille', 'rodrigue'],
  123. 'private' => true,
  124. ];
  125. $request = Request::createFromEnvironment($env);
  126. $request = $request->withParsedBody($update);
  127. $response = $this->controller->putLink($request, new Response(), ['id' => $id]);
  128. $this->assertEquals(200, $response->getStatusCode());
  129. $data = json_decode((string) $response->getBody(), true);
  130. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  131. $this->assertEquals($id, $data['id']);
  132. $this->assertEquals('WDWyig', $data['shorturl']);
  133. $this->assertEquals('http://somewhere.else', $data['url']);
  134. $this->assertEquals('Le Cid', $data['title']);
  135. $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
  136. $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
  137. $this->assertEquals(true, $data['private']);
  138. $this->assertEquals(
  139. \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
  140. \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
  141. );
  142. $this->assertTrue(
  143. new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
  144. );
  145. }
  146. /**
  147. * Test link update with an existing URL: 409 Conflict with the existing link as body
  148. */
  149. public function testPutLinkDuplicate()
  150. {
  151. $link = [
  152. 'url' => 'mediagoblin.org/',
  153. 'title' => 'new entry',
  154. 'description' => 'shaare description',
  155. 'tags' => ['one', 'two'],
  156. 'private' => true,
  157. ];
  158. $env = Environment::mock([
  159. 'REQUEST_METHOD' => 'PUT',
  160. 'CONTENT_TYPE' => 'application/json'
  161. ]);
  162. $request = Request::createFromEnvironment($env);
  163. $request = $request->withParsedBody($link);
  164. $response = $this->controller->putLink($request, new Response(), ['id' => 41]);
  165. $this->assertEquals(409, $response->getStatusCode());
  166. $data = json_decode((string) $response->getBody(), true);
  167. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  168. $this->assertEquals(7, $data['id']);
  169. $this->assertEquals('IuWvgA', $data['shorturl']);
  170. $this->assertEquals('http://mediagoblin.org/', $data['url']);
  171. $this->assertEquals('MediaGoblin', $data['title']);
  172. $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
  173. $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
  174. $this->assertEquals(false, $data['private']);
  175. $this->assertEquals(
  176. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
  177. \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
  178. );
  179. $this->assertEquals(
  180. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
  181. \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
  182. );
  183. }
  184. /**
  185. * Test link update on non existent link => ApiLinkNotFoundException.
  186. *
  187. * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
  188. * @expectedExceptionMessage Link not found
  189. */
  190. public function testGetLink404()
  191. {
  192. $env = Environment::mock([
  193. 'REQUEST_METHOD' => 'PUT',
  194. ]);
  195. $request = Request::createFromEnvironment($env);
  196. $this->controller->putLink($request, new Response(), ['id' => -1]);
  197. }
  198. }