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