2016-12-15 10:13:00 +01:00
|
|
|
<?php
|
|
|
|
namespace Shaarli\Api\Controllers;
|
|
|
|
|
2020-09-26 14:18:01 +02:00
|
|
|
use malkusch\lock\mutex\NoMutex;
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\Bookmark\BookmarkFileService;
|
2017-03-03 23:06:12 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\History;
|
2021-01-20 15:59:00 +01:00
|
|
|
use Shaarli\Plugin\PluginManager;
|
2020-09-29 14:41:40 +02:00
|
|
|
use Shaarli\TestCase;
|
2016-12-15 10:13:00 +01:00
|
|
|
use Slim\Container;
|
|
|
|
use Slim\Http\Environment;
|
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class InfoTest
|
2018-10-13 00:35:47 +02:00
|
|
|
*
|
2016-12-15 10:13:00 +01:00
|
|
|
* Test REST API controller Info.
|
2018-10-13 00:35:47 +02:00
|
|
|
*
|
2016-12-15 10:13:00 +01:00
|
|
|
* @package Api\Controllers
|
|
|
|
*/
|
2020-01-17 21:34:12 +01:00
|
|
|
class InfoTest extends TestCase
|
2016-12-15 10:13:00 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string datastore to test write operations
|
|
|
|
*/
|
|
|
|
protected static $testDatastore = 'sandbox/datastore.php';
|
|
|
|
|
|
|
|
/**
|
2017-03-03 23:06:12 +01:00
|
|
|
* @var ConfigManager instance
|
2016-12-15 10:13:00 +01:00
|
|
|
*/
|
|
|
|
protected $conf;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \ReferenceLinkDB instance.
|
|
|
|
*/
|
|
|
|
protected $refDB = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Container instance.
|
|
|
|
*/
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Info controller instance.
|
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Before every test, instantiate a new Api with its config, plugins and bookmarks.
|
2016-12-15 10:13:00 +01:00
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function setUp(): void
|
2016-12-15 10:13:00 +01:00
|
|
|
{
|
2020-09-26 14:18:01 +02:00
|
|
|
$mutex = new NoMutex();
|
2020-01-17 21:34:12 +01:00
|
|
|
$this->conf = new ConfigManager('tests/utils/config/configJson');
|
|
|
|
$this->conf->set('resource.datastore', self::$testDatastore);
|
2016-12-15 10:13:00 +01:00
|
|
|
$this->refDB = new \ReferenceLinkDB();
|
|
|
|
$this->refDB->write(self::$testDatastore);
|
2021-01-20 15:59:00 +01:00
|
|
|
$this->pluginManager = new PluginManager($this->conf);
|
2020-01-17 21:34:12 +01:00
|
|
|
$history = new History('sandbox/history.php');
|
|
|
|
|
2016-12-15 10:13:00 +01:00
|
|
|
$this->container = new Container();
|
|
|
|
$this->container['conf'] = $this->conf;
|
2021-01-20 15:59:00 +01:00
|
|
|
$this->container['db'] = new BookmarkFileService(
|
|
|
|
$this->conf,
|
|
|
|
$this->pluginManager,
|
|
|
|
$history,
|
|
|
|
$mutex,
|
|
|
|
true
|
|
|
|
);
|
2017-05-07 16:50:20 +02:00
|
|
|
$this->container['history'] = null;
|
2016-12-15 10:13:00 +01:00
|
|
|
|
|
|
|
$this->controller = new Info($this->container);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After every test, remove the test datastore.
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function tearDown(): void
|
2016-12-15 10:13:00 +01:00
|
|
|
{
|
|
|
|
@unlink(self::$testDatastore);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test /info service.
|
|
|
|
*/
|
|
|
|
public function testGetInfo()
|
|
|
|
{
|
|
|
|
$env = Environment::mock([
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
]);
|
|
|
|
$request = Request::createFromEnvironment($env);
|
|
|
|
|
|
|
|
$response = $this->controller->getInfo($request, new Response());
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$data = json_decode((string) $response->getBody(), true);
|
|
|
|
|
2017-04-01 12:17:37 +02:00
|
|
|
$this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']);
|
2016-12-15 10:13:00 +01:00
|
|
|
$this->assertEquals(2, $data['private_counter']);
|
|
|
|
$this->assertEquals('Shaarli', $data['settings']['title']);
|
|
|
|
$this->assertEquals('?', $data['settings']['header_link']);
|
2020-01-17 21:34:12 +01:00
|
|
|
$this->assertEquals('Europe/Paris', $data['settings']['timezone']);
|
2017-03-03 23:06:12 +01:00
|
|
|
$this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
|
2020-01-17 21:34:12 +01:00
|
|
|
$this->assertEquals(true, $data['settings']['default_private_links']);
|
2016-12-15 10:13:00 +01:00
|
|
|
|
2020-01-17 21:34:12 +01:00
|
|
|
$title = 'My bookmarks';
|
2016-12-15 10:13:00 +01:00
|
|
|
$headerLink = 'http://shaarli.tld';
|
|
|
|
$timezone = 'Europe/Paris';
|
|
|
|
$enabledPlugins = array('foo', 'bar');
|
|
|
|
$defaultPrivateLinks = true;
|
|
|
|
$this->conf->set('general.title', $title);
|
|
|
|
$this->conf->set('general.header_link', $headerLink);
|
|
|
|
$this->conf->set('general.timezone', $timezone);
|
|
|
|
$this->conf->set('general.enabled_plugins', $enabledPlugins);
|
|
|
|
$this->conf->set('privacy.default_private_links', $defaultPrivateLinks);
|
|
|
|
|
|
|
|
$response = $this->controller->getInfo($request, new Response());
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$data = json_decode((string) $response->getBody(), true);
|
|
|
|
|
2017-04-01 12:17:37 +02:00
|
|
|
$this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']);
|
2016-12-15 10:13:00 +01:00
|
|
|
$this->assertEquals(2, $data['private_counter']);
|
|
|
|
$this->assertEquals($title, $data['settings']['title']);
|
|
|
|
$this->assertEquals($headerLink, $data['settings']['header_link']);
|
|
|
|
$this->assertEquals($timezone, $data['settings']['timezone']);
|
|
|
|
$this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']);
|
|
|
|
$this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']);
|
|
|
|
}
|
|
|
|
}
|