]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Shaarli\Api\Controllers; | |
4 | ||
5 | use malkusch\lock\mutex\NoMutex; | |
6 | use Shaarli\Bookmark\Bookmark; | |
7 | use Shaarli\Bookmark\BookmarkFileService; | |
8 | use Shaarli\Config\ConfigManager; | |
9 | use Shaarli\History; | |
10 | use Shaarli\Plugin\PluginManager; | |
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 | */ | |
25 | class GetLinkIdTest extends \Shaarli\TestCase | |
26 | { | |
27 | /** | |
28 | * @var string datastore to test write operations | |
29 | */ | |
30 | protected static $testDatastore = 'sandbox/datastore.php'; | |
31 | ||
32 | /** | |
33 | * @var ConfigManager instance | |
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 | /** | |
58 | * Before each test, instantiate a new Api with its config, plugins and bookmarks. | |
59 | */ | |
60 | protected function setUp(): void | |
61 | { | |
62 | $mutex = new NoMutex(); | |
63 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | |
64 | $this->conf->set('resource.datastore', self::$testDatastore); | |
65 | $this->refDB = new \ReferenceLinkDB(); | |
66 | $this->refDB->write(self::$testDatastore); | |
67 | $history = new History('sandbox/history.php'); | |
68 | ||
69 | $this->container = new Container(); | |
70 | $this->container['conf'] = $this->conf; | |
71 | $pluginManager = new PluginManager($this->conf); | |
72 | $this->container['db'] = new BookmarkFileService( | |
73 | $this->conf, | |
74 | $pluginManager, | |
75 | $history, | |
76 | $mutex, | |
77 | true | |
78 | ); | |
79 | $this->container['history'] = null; | |
80 | ||
81 | $this->controller = new Links($this->container); | |
82 | } | |
83 | ||
84 | /** | |
85 | * After each test, remove the test datastore. | |
86 | */ | |
87 | protected function tearDown(): void | |
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 | |
115 | $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']); | |
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( | |
125 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), | |
126 | $data['created'] | |
127 | ); | |
128 | $this->assertEmpty($data['updated']); | |
129 | } | |
130 | ||
131 | /** | |
132 | * Test basic getLink service: get non existent link => ApiLinkNotFoundException. | |
133 | */ | |
134 | public function testGetLink404() | |
135 | { | |
136 | $this->expectException(\Shaarli\Api\Exceptions\ApiLinkNotFoundException::class); | |
137 | $this->expectExceptionMessage('Link not found'); | |
138 | ||
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 | } |