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