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