]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/info/InfoTest.php
Merge tag 'v0.12.1' into latest
[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;
a5a9cf23 8use Shaarli\TestCase;
18e67967
A
9use Slim\Container;
10use Slim\Http\Environment;
11use Slim\Http\Request;
12use Slim\Http\Response;
13
14/**
15 * Class InfoTest
067c2dd8 16 *
18e67967 17 * Test REST API controller Info.
067c2dd8 18 *
18e67967
A
19 * @package Api\Controllers
20 */
e26e2060 21class InfoTest extends TestCase
18e67967
A
22{
23 /**
24 * @var string datastore to test write operations
25 */
26 protected static $testDatastore = 'sandbox/datastore.php';
27
28 /**
3c66e564 29 * @var ConfigManager instance
18e67967
A
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 /**
e26e2060 49 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
18e67967 50 */
8f60e120 51 protected function setUp(): void
18e67967 52 {
fd1ddad9 53 $mutex = new NoMutex();
e26e2060
A
54 $this->conf = new ConfigManager('tests/utils/config/configJson');
55 $this->conf->set('resource.datastore', self::$testDatastore);
18e67967
A
56 $this->refDB = new \ReferenceLinkDB();
57 $this->refDB->write(self::$testDatastore);
58
e26e2060
A
59 $history = new History('sandbox/history.php');
60
18e67967
A
61 $this->container = new Container();
62 $this->container['conf'] = $this->conf;
fd1ddad9 63 $this->container['db'] = new BookmarkFileService($this->conf, $history, $mutex, true);
813849e5 64 $this->container['history'] = null;
18e67967
A
65
66 $this->controller = new Info($this->container);
67 }
68
69 /**
70 * After every test, remove the test datastore.
71 */
8f60e120 72 protected function tearDown(): void
18e67967
A
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
7d86f40b 91 $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']);
18e67967
A
92 $this->assertEquals(2, $data['private_counter']);
93 $this->assertEquals('Shaarli', $data['settings']['title']);
94 $this->assertEquals('?', $data['settings']['header_link']);
e26e2060 95 $this->assertEquals('Europe/Paris', $data['settings']['timezone']);
3c66e564 96 $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
e26e2060 97 $this->assertEquals(true, $data['settings']['default_private_links']);
18e67967 98
e26e2060 99 $title = 'My bookmarks';
18e67967
A
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
7d86f40b 114 $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']);
18e67967
A
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}