PutLinkTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 ConfigManager instance
  16. */
  17. protected $conf;
  18. /**
  19. * @var \ReferenceLinkDB instance.
  20. */
  21. protected $refDB = null;
  22. /**
  23. * @var Container instance.
  24. */
  25. protected $container;
  26. /**
  27. * @var Links controller instance.
  28. */
  29. protected $controller;
  30. /**
  31. * Number of JSON field per link.
  32. */
  33. const NB_FIELDS_LINK = 9;
  34. /**
  35. * Before every test, instantiate a new Api with its config, plugins and links.
  36. */
  37. public function setUp()
  38. {
  39. $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
  40. $this->refDB = new \ReferenceLinkDB();
  41. $this->refDB->write(self::$testDatastore);
  42. $this->container = new Container();
  43. $this->container['conf'] = $this->conf;
  44. $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
  45. $this->controller = new Links($this->container);
  46. // Used by index_url().
  47. $this->controller->getCi()['environment'] = [
  48. 'SERVER_NAME' => 'domain.tld',
  49. 'SERVER_PORT' => 80,
  50. 'SCRIPT_NAME' => '/',
  51. ];
  52. }
  53. /**
  54. * After every test, remove the test datastore.
  55. */
  56. public function tearDown()
  57. {
  58. @unlink(self::$testDatastore);
  59. }
  60. /**
  61. * Test link update without value: reset the link to default values
  62. */
  63. public function testPutLinkMinimal()
  64. {
  65. $env = Environment::mock([
  66. 'REQUEST_METHOD' => 'PUT',
  67. ]);
  68. $id = '41';
  69. $request = Request::createFromEnvironment($env);
  70. $response = $this->controller->putLink($request, new Response(), ['id' => $id]);
  71. $this->assertEquals(200, $response->getStatusCode());
  72. $data = json_decode((string) $response->getBody(), true);
  73. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  74. $this->assertEquals($id, $data['id']);
  75. $this->assertEquals('WDWyig', $data['shorturl']);
  76. $this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
  77. $this->assertEquals('?WDWyig', $data['title']);
  78. $this->assertEquals('', $data['description']);
  79. $this->assertEquals([], $data['tags']);
  80. $this->assertEquals(false, $data['private']);
  81. $this->assertEquals(
  82. \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
  83. \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
  84. );
  85. $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']));
  86. }
  87. /**
  88. * Test link update with new values
  89. */
  90. public function testPutLinkWithValues()
  91. {
  92. $env = Environment::mock([
  93. 'REQUEST_METHOD' => 'PUT',
  94. 'CONTENT_TYPE' => 'application/json'
  95. ]);
  96. $id = 41;
  97. $update = [
  98. 'url' => 'http://somewhere.else',
  99. 'title' => 'Le Cid',
  100. 'description' => 'Percé jusques au fond du cœur [...]',
  101. 'tags' => ['corneille', 'rodrigue'],
  102. 'private' => true,
  103. ];
  104. $request = Request::createFromEnvironment($env);
  105. $request = $request->withParsedBody($update);
  106. $response = $this->controller->putLink($request, new Response(), ['id' => $id]);
  107. $this->assertEquals(200, $response->getStatusCode());
  108. $data = json_decode((string) $response->getBody(), true);
  109. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  110. $this->assertEquals($id, $data['id']);
  111. $this->assertEquals('WDWyig', $data['shorturl']);
  112. $this->assertEquals('http://somewhere.else', $data['url']);
  113. $this->assertEquals('Le Cid', $data['title']);
  114. $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
  115. $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
  116. $this->assertEquals(true, $data['private']);
  117. $this->assertEquals(
  118. \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
  119. \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
  120. );
  121. $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']));
  122. }
  123. /**
  124. * Test link update with an existing URL: 409 Conflict with the existing link as body
  125. */
  126. public function testPutLinkDuplicate()
  127. {
  128. $link = [
  129. 'url' => 'mediagoblin.org/',
  130. 'title' => 'new entry',
  131. 'description' => 'shaare description',
  132. 'tags' => ['one', 'two'],
  133. 'private' => true,
  134. ];
  135. $env = Environment::mock([
  136. 'REQUEST_METHOD' => 'PUT',
  137. 'CONTENT_TYPE' => 'application/json'
  138. ]);
  139. $request = Request::createFromEnvironment($env);
  140. $request = $request->withParsedBody($link);
  141. $response = $this->controller->putLink($request, new Response(), ['id' => 41]);
  142. $this->assertEquals(409, $response->getStatusCode());
  143. $data = json_decode((string) $response->getBody(), true);
  144. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  145. $this->assertEquals(7, $data['id']);
  146. $this->assertEquals('IuWvgA', $data['shorturl']);
  147. $this->assertEquals('http://mediagoblin.org/', $data['url']);
  148. $this->assertEquals('MediaGoblin', $data['title']);
  149. $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
  150. $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
  151. $this->assertEquals(false, $data['private']);
  152. $this->assertEquals(
  153. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
  154. \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
  155. );
  156. $this->assertEquals(
  157. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
  158. \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
  159. );
  160. }
  161. /**
  162. * Test link update on non existent link => ApiLinkNotFoundException.
  163. *
  164. * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
  165. * @expectedExceptionMessage Link not found
  166. */
  167. public function testGetLink404()
  168. {
  169. $env = Environment::mock([
  170. 'REQUEST_METHOD' => 'PUT',
  171. ]);
  172. $request = Request::createFromEnvironment($env);
  173. $this->controller->putLink($request, new Response(), ['id' => -1]);
  174. }
  175. }