]>
Commit | Line | Data |
---|---|---|
18e67967 | 1 | <?php |
18e67967 A |
2 | namespace Shaarli\Api\Controllers; |
3 | ||
e26e2060 | 4 | use Shaarli\Bookmark\BookmarkFileService; |
3c66e564 | 5 | use Shaarli\Config\ConfigManager; |
e26e2060 | 6 | use Shaarli\History; |
a5a9cf23 | 7 | use Shaarli\TestCase; |
18e67967 A |
8 | use Slim\Container; |
9 | use Slim\Http\Environment; | |
10 | use Slim\Http\Request; | |
11 | use Slim\Http\Response; | |
12 | ||
13 | /** | |
14 | * Class InfoTest | |
067c2dd8 | 15 | * |
18e67967 | 16 | * Test REST API controller Info. |
067c2dd8 | 17 | * |
18e67967 A |
18 | * @package Api\Controllers |
19 | */ | |
e26e2060 | 20 | class InfoTest extends TestCase |
18e67967 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 |
18e67967 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 Info controller instance. | |
44 | */ | |
45 | protected $controller; | |
46 | ||
47 | /** | |
e26e2060 | 48 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. |
18e67967 | 49 | */ |
8f60e120 | 50 | protected function setUp(): void |
18e67967 | 51 | { |
e26e2060 A |
52 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
53 | $this->conf->set('resource.datastore', self::$testDatastore); | |
18e67967 A |
54 | $this->refDB = new \ReferenceLinkDB(); |
55 | $this->refDB->write(self::$testDatastore); | |
56 | ||
e26e2060 A |
57 | $history = new History('sandbox/history.php'); |
58 | ||
18e67967 A |
59 | $this->container = new Container(); |
60 | $this->container['conf'] = $this->conf; | |
e26e2060 | 61 | $this->container['db'] = new BookmarkFileService($this->conf, $history, true); |
813849e5 | 62 | $this->container['history'] = null; |
18e67967 A |
63 | |
64 | $this->controller = new Info($this->container); | |
65 | } | |
66 | ||
67 | /** | |
68 | * After every test, remove the test datastore. | |
69 | */ | |
8f60e120 | 70 | protected function tearDown(): void |
18e67967 A |
71 | { |
72 | @unlink(self::$testDatastore); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Test /info service. | |
77 | */ | |
78 | public function testGetInfo() | |
79 | { | |
80 | $env = Environment::mock([ | |
81 | 'REQUEST_METHOD' => 'GET', | |
82 | ]); | |
83 | $request = Request::createFromEnvironment($env); | |
84 | ||
85 | $response = $this->controller->getInfo($request, new Response()); | |
86 | $this->assertEquals(200, $response->getStatusCode()); | |
87 | $data = json_decode((string) $response->getBody(), true); | |
88 | ||
7d86f40b | 89 | $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']); |
18e67967 A |
90 | $this->assertEquals(2, $data['private_counter']); |
91 | $this->assertEquals('Shaarli', $data['settings']['title']); | |
92 | $this->assertEquals('?', $data['settings']['header_link']); | |
e26e2060 | 93 | $this->assertEquals('Europe/Paris', $data['settings']['timezone']); |
3c66e564 | 94 | $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']); |
e26e2060 | 95 | $this->assertEquals(true, $data['settings']['default_private_links']); |
18e67967 | 96 | |
e26e2060 | 97 | $title = 'My bookmarks'; |
18e67967 A |
98 | $headerLink = 'http://shaarli.tld'; |
99 | $timezone = 'Europe/Paris'; | |
100 | $enabledPlugins = array('foo', 'bar'); | |
101 | $defaultPrivateLinks = true; | |
102 | $this->conf->set('general.title', $title); | |
103 | $this->conf->set('general.header_link', $headerLink); | |
104 | $this->conf->set('general.timezone', $timezone); | |
105 | $this->conf->set('general.enabled_plugins', $enabledPlugins); | |
106 | $this->conf->set('privacy.default_private_links', $defaultPrivateLinks); | |
107 | ||
108 | $response = $this->controller->getInfo($request, new Response()); | |
109 | $this->assertEquals(200, $response->getStatusCode()); | |
110 | $data = json_decode((string) $response->getBody(), true); | |
111 | ||
7d86f40b | 112 | $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']); |
18e67967 A |
113 | $this->assertEquals(2, $data['private_counter']); |
114 | $this->assertEquals($title, $data['settings']['title']); | |
115 | $this->assertEquals($headerLink, $data['settings']['header_link']); | |
116 | $this->assertEquals($timezone, $data['settings']['timezone']); | |
117 | $this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']); | |
118 | $this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']); | |
119 | } | |
120 | } |