]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/info/InfoTest.php
New plugin hook: ability to add custom filters to Shaarli search engine
[github/shaarli/Shaarli.git] / tests / api / controllers / info / InfoTest.php
CommitLineData
18e67967 1<?php
18e67967
A
2namespace Shaarli\Api\Controllers;
3
fd1ddad9 4use malkusch\lock\mutex\NoMutex;
e26e2060 5use Shaarli\Bookmark\BookmarkFileService;
3c66e564 6use Shaarli\Config\ConfigManager;
e26e2060 7use Shaarli\History;
bcba6bd3 8use Shaarli\Plugin\PluginManager;
a5a9cf23 9use Shaarli\TestCase;
18e67967
A
10use Slim\Container;
11use Slim\Http\Environment;
12use Slim\Http\Request;
13use Slim\Http\Response;
14
15/**
16 * Class InfoTest
067c2dd8 17 *
18e67967 18 * Test REST API controller Info.
067c2dd8 19 *
18e67967
A
20 * @package Api\Controllers
21 */
e26e2060 22class InfoTest extends TestCase
18e67967
A
23{
24 /**
25 * @var string datastore to test write operations
26 */
27 protected static $testDatastore = 'sandbox/datastore.php';
28
29 /**
3c66e564 30 * @var ConfigManager instance
18e67967
A
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 /**
e26e2060 50 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
18e67967 51 */
8f60e120 52 protected function setUp(): void
18e67967 53 {
fd1ddad9 54 $mutex = new NoMutex();
e26e2060
A
55 $this->conf = new ConfigManager('tests/utils/config/configJson');
56 $this->conf->set('resource.datastore', self::$testDatastore);
18e67967
A
57 $this->refDB = new \ReferenceLinkDB();
58 $this->refDB->write(self::$testDatastore);
bcba6bd3 59 $this->pluginManager = new PluginManager($this->conf);
e26e2060
A
60 $history = new History('sandbox/history.php');
61
18e67967
A
62 $this->container = new Container();
63 $this->container['conf'] = $this->conf;
bcba6bd3
A
64 $this->container['db'] = new BookmarkFileService(
65 $this->conf,
66 $this->pluginManager,
67 $history,
68 $mutex,
69 true
70 );
813849e5 71 $this->container['history'] = null;
18e67967
A
72
73 $this->controller = new Info($this->container);
74 }
75
76 /**
77 * After every test, remove the test datastore.
78 */
8f60e120 79 protected function tearDown(): void
18e67967
A
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
7d86f40b 98 $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']);
18e67967
A
99 $this->assertEquals(2, $data['private_counter']);
100 $this->assertEquals('Shaarli', $data['settings']['title']);
101 $this->assertEquals('?', $data['settings']['header_link']);
e26e2060 102 $this->assertEquals('Europe/Paris', $data['settings']['timezone']);
3c66e564 103 $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
e26e2060 104 $this->assertEquals(true, $data['settings']['default_private_links']);
18e67967 105
e26e2060 106 $title = 'My bookmarks';
18e67967
A
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
7d86f40b 121 $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']);
18e67967
A
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}