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