PostLinkTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Shaarli\Api\Controllers;
  3. use PHPUnit\Framework\TestCase;
  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. use Slim\Router;
  10. /**
  11. * Class PostLinkTest
  12. *
  13. * Test POST Link REST API service.
  14. *
  15. * @package Shaarli\Api\Controllers
  16. */
  17. class PostLinkTest extends TestCase
  18. {
  19. /**
  20. * @var string datastore to test write operations
  21. */
  22. protected static $testDatastore = 'sandbox/datastore.php';
  23. /**
  24. * @var string datastore to test write operations
  25. */
  26. protected static $testHistory = 'sandbox/history.php';
  27. /**
  28. * @var ConfigManager instance
  29. */
  30. protected $conf;
  31. /**
  32. * @var \ReferenceLinkDB instance.
  33. */
  34. protected $refDB = null;
  35. /**
  36. * @var \History instance.
  37. */
  38. protected $history;
  39. /**
  40. * @var Container instance.
  41. */
  42. protected $container;
  43. /**
  44. * @var Links controller instance.
  45. */
  46. protected $controller;
  47. /**
  48. * Number of JSON field per link.
  49. */
  50. const NB_FIELDS_LINK = 9;
  51. /**
  52. * Before every test, instantiate a new Api with its config, plugins and links.
  53. */
  54. public function setUp()
  55. {
  56. $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
  57. $this->refDB = new \ReferenceLinkDB();
  58. $this->refDB->write(self::$testDatastore);
  59. $refHistory = new \ReferenceHistory();
  60. $refHistory->write(self::$testHistory);
  61. $this->history = new \History(self::$testHistory);
  62. $this->container = new Container();
  63. $this->container['conf'] = $this->conf;
  64. $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
  65. $this->container['history'] = new \History(self::$testHistory);
  66. $this->controller = new Links($this->container);
  67. $mock = $this->createMock(Router::class);
  68. $mock->expects($this->any())
  69. ->method('relativePathFor')
  70. ->willReturn('api/v1/links/1');
  71. // affect @property-read... seems to work
  72. $this->controller->getCi()->router = $mock;
  73. // Used by index_url().
  74. $this->controller->getCi()['environment'] = [
  75. 'SERVER_NAME' => 'domain.tld',
  76. 'SERVER_PORT' => 80,
  77. 'SCRIPT_NAME' => '/',
  78. ];
  79. }
  80. /**
  81. * After every test, remove the test datastore.
  82. */
  83. public function tearDown()
  84. {
  85. @unlink(self::$testDatastore);
  86. @unlink(self::$testHistory);
  87. }
  88. /**
  89. * Test link creation without any field: creates a blank note.
  90. */
  91. public function testPostLinkMinimal()
  92. {
  93. $env = Environment::mock([
  94. 'REQUEST_METHOD' => 'POST',
  95. ]);
  96. $request = Request::createFromEnvironment($env);
  97. $response = $this->controller->postLink($request, new Response());
  98. $this->assertEquals(201, $response->getStatusCode());
  99. $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
  100. $data = json_decode((string) $response->getBody(), true);
  101. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  102. $this->assertEquals(43, $data['id']);
  103. $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
  104. $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']);
  105. $this->assertEquals('?' . $data['shorturl'], $data['title']);
  106. $this->assertEquals('', $data['description']);
  107. $this->assertEquals([], $data['tags']);
  108. $this->assertEquals(false, $data['private']);
  109. $this->assertTrue(
  110. new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
  111. );
  112. $this->assertEquals('', $data['updated']);
  113. $historyEntry = $this->history->getHistory()[0];
  114. $this->assertEquals(\History::CREATED, $historyEntry['event']);
  115. $this->assertTrue(
  116. (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
  117. );
  118. $this->assertEquals(43, $historyEntry['id']);
  119. }
  120. /**
  121. * Test link creation with all available fields.
  122. */
  123. public function testPostLinkFull()
  124. {
  125. $link = [
  126. 'url' => 'website.tld/test?foo=bar',
  127. 'title' => 'new entry',
  128. 'description' => 'shaare description',
  129. 'tags' => ['one', 'two'],
  130. 'private' => true,
  131. ];
  132. $env = Environment::mock([
  133. 'REQUEST_METHOD' => 'POST',
  134. 'CONTENT_TYPE' => 'application/json'
  135. ]);
  136. $request = Request::createFromEnvironment($env);
  137. $request = $request->withParsedBody($link);
  138. $response = $this->controller->postLink($request, new Response());
  139. $this->assertEquals(201, $response->getStatusCode());
  140. $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]);
  141. $data = json_decode((string) $response->getBody(), true);
  142. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  143. $this->assertEquals(43, $data['id']);
  144. $this->assertRegExp('/[\w-_]{6}/', $data['shorturl']);
  145. $this->assertEquals('http://' . $link['url'], $data['url']);
  146. $this->assertEquals($link['title'], $data['title']);
  147. $this->assertEquals($link['description'], $data['description']);
  148. $this->assertEquals($link['tags'], $data['tags']);
  149. $this->assertEquals(true, $data['private']);
  150. $this->assertTrue(
  151. new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
  152. );
  153. $this->assertEquals('', $data['updated']);
  154. }
  155. /**
  156. * Test link creation with an existing link (duplicate URL). Should return a 409 HTTP error and the existing link.
  157. */
  158. public function testPostLinkDuplicate()
  159. {
  160. $link = [
  161. 'url' => 'mediagoblin.org/',
  162. 'title' => 'new entry',
  163. 'description' => 'shaare description',
  164. 'tags' => ['one', 'two'],
  165. 'private' => true,
  166. ];
  167. $env = Environment::mock([
  168. 'REQUEST_METHOD' => 'POST',
  169. 'CONTENT_TYPE' => 'application/json'
  170. ]);
  171. $request = Request::createFromEnvironment($env);
  172. $request = $request->withParsedBody($link);
  173. $response = $this->controller->postLink($request, new Response());
  174. $this->assertEquals(409, $response->getStatusCode());
  175. $data = json_decode((string) $response->getBody(), true);
  176. $this->assertEquals(self::NB_FIELDS_LINK, count($data));
  177. $this->assertEquals(7, $data['id']);
  178. $this->assertEquals('IuWvgA', $data['shorturl']);
  179. $this->assertEquals('http://mediagoblin.org/', $data['url']);
  180. $this->assertEquals('MediaGoblin', $data['title']);
  181. $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
  182. $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
  183. $this->assertEquals(false, $data['private']);
  184. $this->assertEquals(
  185. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
  186. \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
  187. );
  188. $this->assertEquals(
  189. \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
  190. \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
  191. );
  192. }
  193. }