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