2 namespace Shaarli\Api\Controllers
;
4 use PHPUnit\Framework\TestCase
;
5 use Shaarli\Bookmark\BookmarkFileService
;
6 use Shaarli\Config\ConfigManager
;
9 use Slim\Http\Environment
;
10 use Slim\Http\Request
;
11 use Slim\Http\Response
;
16 * Test REST API controller Info.
18 * @package Api\Controllers
20 class InfoTest
extends TestCase
23 * @var string datastore to test write operations
25 protected static $testDatastore = 'sandbox/datastore.php';
28 * @var ConfigManager instance
33 * @var \ReferenceLinkDB instance.
35 protected $refDB = null;
38 * @var Container instance.
43 * @var Info controller instance.
45 protected $controller;
48 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
50 public function setUp()
52 $this->conf
= new ConfigManager('tests/utils/config/configJson');
53 $this->conf
->set('resource.datastore', self
::$testDatastore);
54 $this->refDB
= new \
ReferenceLinkDB();
55 $this->refDB
->write(self
::$testDatastore);
57 $history = new History('sandbox/history.php');
59 $this->container
= new Container();
60 $this->container
['conf'] = $this->conf
;
61 $this->container
['db'] = new BookmarkFileService($this->conf
, $history, true);
62 $this->container
['history'] = null;
64 $this->controller
= new Info($this->container
);
68 * After every test, remove the test datastore.
70 public function tearDown()
72 @unlink(self
::$testDatastore);
78 public function testGetInfo()
80 $env = Environment
::mock([
81 'REQUEST_METHOD' => 'GET',
83 $request = Request
::createFromEnvironment($env);
85 $response = $this->controller
->getInfo($request, new Response());
86 $this->assertEquals(200, $response->getStatusCode());
87 $data = json_decode((string) $response->getBody(), true);
89 $this->assertEquals(\ReferenceLinkDB
::$NB_LINKS_TOTAL, $data['global_counter']);
90 $this->assertEquals(2, $data['private_counter']);
91 $this->assertEquals('Shaarli', $data['settings']['title']);
92 $this->assertEquals('?', $data['settings']['header_link']);
93 $this->assertEquals('Europe/Paris', $data['settings']['timezone']);
94 $this->assertEquals(ConfigManager
::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
95 $this->assertEquals(true, $data['settings']['default_private_links']);
97 $title = 'My bookmarks';
98 $headerLink = 'http://shaarli.tld';
99 $timezone = 'Europe/Paris';
100 $enabledPlugins = array('foo', 'bar');
101 $defaultPrivateLinks = true;
102 $this->conf
->set('general.title', $title);
103 $this->conf
->set('general.header_link', $headerLink);
104 $this->conf
->set('general.timezone', $timezone);
105 $this->conf
->set('general.enabled_plugins', $enabledPlugins);
106 $this->conf
->set('privacy.default_private_links', $defaultPrivateLinks);
108 $response = $this->controller
->getInfo($request, new Response());
109 $this->assertEquals(200, $response->getStatusCode());
110 $data = json_decode((string) $response->getBody(), true);
112 $this->assertEquals(\ReferenceLinkDB
::$NB_LINKS_TOTAL, $data['global_counter']);
113 $this->assertEquals(2, $data['private_counter']);
114 $this->assertEquals($title, $data['settings']['title']);
115 $this->assertEquals($headerLink, $data['settings']['header_link']);
116 $this->assertEquals($timezone, $data['settings']['timezone']);
117 $this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']);
118 $this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']);