PostLinkTest.php 7.3 KB

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