2 namespace Shaarli\Api\Controllers
;
4 use malkusch\lock\mutex\NoMutex
;
5 use Shaarli\Bookmark\BookmarkFileService
;
6 use Shaarli\Config\ConfigManager
;
10 use Slim\Http\Environment
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
17 * Test REST API controller Info.
19 * @package Api\Controllers
21 class InfoTest
extends TestCase
24 * @var string datastore to test write operations
26 protected static $testDatastore = 'sandbox/datastore.php';
29 * @var ConfigManager instance
34 * @var \ReferenceLinkDB instance.
36 protected $refDB = null;
39 * @var Container instance.
44 * @var Info controller instance.
46 protected $controller;
49 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
51 protected function setUp(): void
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);
59 $history = new History('sandbox/history.php');
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;
66 $this->controller
= new Info($this->container
);
70 * After every test, remove the test datastore.
72 protected function tearDown(): void
74 @unlink(self
::$testDatastore);
80 public function testGetInfo()
82 $env = Environment
::mock([
83 'REQUEST_METHOD' => 'GET',
85 $request = Request
::createFromEnvironment($env);
87 $response = $this->controller
->getInfo($request, new Response());
88 $this->assertEquals(200, $response->getStatusCode());
89 $data = json_decode((string) $response->getBody(), true);
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']);
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);
110 $response = $this->controller
->getInfo($request, new Response());
111 $this->assertEquals(200, $response->getStatusCode());
112 $data = json_decode((string) $response->getBody(), true);
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']);