2 namespace Shaarli\Api\Controllers
;
4 use malkusch\lock\mutex\NoMutex
;
5 use Shaarli\Bookmark\BookmarkFileService
;
6 use Shaarli\Config\ConfigManager
;
8 use Shaarli\Plugin\PluginManager
;
11 use Slim\Http\Environment
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
18 * Test REST API controller Info.
20 * @package Api\Controllers
22 class InfoTest
extends TestCase
25 * @var string datastore to test write operations
27 protected static $testDatastore = 'sandbox/datastore.php';
30 * @var ConfigManager instance
35 * @var \ReferenceLinkDB instance.
37 protected $refDB = null;
40 * @var Container instance.
45 * @var Info controller instance.
47 protected $controller;
50 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
52 protected function setUp(): void
54 $mutex = new NoMutex();
55 $this->conf
= new ConfigManager('tests/utils/config/configJson');
56 $this->conf
->set('resource.datastore', self
::$testDatastore);
57 $this->refDB
= new \
ReferenceLinkDB();
58 $this->refDB
->write(self
::$testDatastore);
59 $this->pluginManager
= new PluginManager($this->conf
);
60 $history = new History('sandbox/history.php');
62 $this->container
= new Container();
63 $this->container
['conf'] = $this->conf
;
64 $this->container
['db'] = new BookmarkFileService(
71 $this->container
['history'] = null;
73 $this->controller
= new Info($this->container
);
77 * After every test, remove the test datastore.
79 protected function tearDown(): void
81 @unlink(self
::$testDatastore);
87 public function testGetInfo()
89 $env = Environment
::mock([
90 'REQUEST_METHOD' => 'GET',
92 $request = Request
::createFromEnvironment($env);
94 $response = $this->controller
->getInfo($request, new Response());
95 $this->assertEquals(200, $response->getStatusCode());
96 $data = json_decode((string) $response->getBody(), true);
98 $this->assertEquals(\ReferenceLinkDB
::$NB_LINKS_TOTAL, $data['global_counter']);
99 $this->assertEquals(2, $data['private_counter']);
100 $this->assertEquals('Shaarli', $data['settings']['title']);
101 $this->assertEquals('?', $data['settings']['header_link']);
102 $this->assertEquals('Europe/Paris', $data['settings']['timezone']);
103 $this->assertEquals(ConfigManager
::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
104 $this->assertEquals(true, $data['settings']['default_private_links']);
106 $title = 'My bookmarks';
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);
117 $response = $this->controller
->getInfo($request, new Response());
118 $this->assertEquals(200, $response->getStatusCode());
119 $data = json_decode((string) $response->getBody(), true);
121 $this->assertEquals(\ReferenceLinkDB
::$NB_LINKS_TOTAL, $data['global_counter']);
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']);