]>
Commit | Line | Data |
---|---|---|
16e3d006 A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Api\Controllers; | |
4 | ||
fd1ddad9 | 5 | use malkusch\lock\mutex\NoMutex; |
e26e2060 A |
6 | use Shaarli\Bookmark\Bookmark; |
7 | use Shaarli\Bookmark\BookmarkFileService; | |
3c66e564 | 8 | use Shaarli\Config\ConfigManager; |
e26e2060 | 9 | use Shaarli\History; |
16e3d006 A |
10 | use Slim\Container; |
11 | use Slim\Http\Environment; | |
12 | use Slim\Http\Request; | |
13 | use Slim\Http\Response; | |
14 | ||
15 | /** | |
16 | * Class GetLinkIdTest | |
17 | * | |
18 | * Test getLink by ID API service. | |
19 | * | |
20 | * @see http://shaarli.github.io/api-documentation/#links-link-get | |
21 | * | |
22 | * @package Shaarli\Api\Controllers | |
23 | */ | |
a5a9cf23 | 24 | class GetLinkIdTest extends \Shaarli\TestCase |
16e3d006 A |
25 | { |
26 | /** | |
27 | * @var string datastore to test write operations | |
28 | */ | |
29 | protected static $testDatastore = 'sandbox/datastore.php'; | |
30 | ||
31 | /** | |
3c66e564 | 32 | * @var ConfigManager instance |
16e3d006 A |
33 | */ |
34 | protected $conf; | |
35 | ||
36 | /** | |
37 | * @var \ReferenceLinkDB instance. | |
38 | */ | |
39 | protected $refDB = null; | |
40 | ||
41 | /** | |
42 | * @var Container instance. | |
43 | */ | |
44 | protected $container; | |
45 | ||
46 | /** | |
47 | * @var Links controller instance. | |
48 | */ | |
49 | protected $controller; | |
50 | ||
51 | /** | |
52 | * Number of JSON fields per link. | |
53 | */ | |
54 | const NB_FIELDS_LINK = 9; | |
55 | ||
56 | /** | |
e26e2060 | 57 | * Before each test, instantiate a new Api with its config, plugins and bookmarks. |
16e3d006 | 58 | */ |
8f60e120 | 59 | protected function setUp(): void |
16e3d006 | 60 | { |
fd1ddad9 | 61 | $mutex = new NoMutex(); |
3c66e564 | 62 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
e26e2060 | 63 | $this->conf->set('resource.datastore', self::$testDatastore); |
16e3d006 A |
64 | $this->refDB = new \ReferenceLinkDB(); |
65 | $this->refDB->write(self::$testDatastore); | |
e26e2060 | 66 | $history = new History('sandbox/history.php'); |
16e3d006 A |
67 | |
68 | $this->container = new Container(); | |
69 | $this->container['conf'] = $this->conf; | |
fd1ddad9 | 70 | $this->container['db'] = new BookmarkFileService($this->conf, $history, $mutex, true); |
813849e5 | 71 | $this->container['history'] = null; |
16e3d006 A |
72 | |
73 | $this->controller = new Links($this->container); | |
74 | } | |
75 | ||
76 | /** | |
77 | * After each test, remove the test datastore. | |
78 | */ | |
8f60e120 | 79 | protected function tearDown(): void |
16e3d006 A |
80 | { |
81 | @unlink(self::$testDatastore); | |
82 | } | |
83 | ||
84 | /** | |
85 | * Test basic getLink service: return link ID=41. | |
86 | */ | |
87 | public function testGetLinkId() | |
88 | { | |
89 | // Used by index_url(). | |
90 | $_SERVER['SERVER_NAME'] = 'domain.tld'; | |
91 | $_SERVER['SERVER_PORT'] = 80; | |
92 | $_SERVER['SCRIPT_NAME'] = '/'; | |
93 | ||
94 | $id = 41; | |
95 | $env = Environment::mock([ | |
96 | 'REQUEST_METHOD' => 'GET', | |
97 | ]); | |
98 | $request = Request::createFromEnvironment($env); | |
99 | ||
100 | $response = $this->controller->getLink($request, new Response(), ['id' => $id]); | |
101 | $this->assertEquals(200, $response->getStatusCode()); | |
102 | $data = json_decode((string) $response->getBody(), true); | |
103 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | |
104 | $this->assertEquals($id, $data['id']); | |
105 | ||
106 | // Check link elements | |
f7f08cee | 107 | $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']); |
16e3d006 A |
108 | $this->assertEquals('WDWyig', $data['shorturl']); |
109 | $this->assertEquals('Link title: @website', $data['title']); | |
110 | $this->assertEquals( | |
111 | 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', | |
112 | $data['description'] | |
113 | ); | |
114 | $this->assertEquals('sTuff', $data['tags'][0]); | |
115 | $this->assertEquals(false, $data['private']); | |
116 | $this->assertEquals( | |
e26e2060 | 117 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), |
16e3d006 A |
118 | $data['created'] |
119 | ); | |
120 | $this->assertEmpty($data['updated']); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Test basic getLink service: get non existent link => ApiLinkNotFoundException. | |
16e3d006 A |
125 | */ |
126 | public function testGetLink404() | |
127 | { | |
b1baca99 A |
128 | $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class); |
129 | $this->expectExceptionMessage('Link not found'); | |
130 | ||
16e3d006 A |
131 | $env = Environment::mock([ |
132 | 'REQUEST_METHOD' => 'GET', | |
133 | ]); | |
134 | $request = Request::createFromEnvironment($env); | |
135 | ||
136 | $this->controller->getLink($request, new Response(), ['id' => -1]); | |
137 | } | |
138 | } |