Merge pull request from ArthurHoaro/web-thumb

Use web-thumbnailer to retrieve thumbnails
This commit is contained in:
ArthurHoaro 2018-07-28 09:41:29 +02:00 committed by GitHub
commit ad5f47adba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 1495 additions and 769 deletions

114
tests/ThumbnailerTest.php Normal file
View file

@ -0,0 +1,114 @@
<?php
namespace Shaarli;
use PHPUnit\Framework\TestCase;
use Shaarli\Config\ConfigManager;
use WebThumbnailer\Application\ConfigManager as WTConfigManager;
/**
* Class ThumbnailerTest
*
* We only make 1 thumb test because:
*
* 1. the thumbnailer library is itself tested
* 2. we don't want to make too many external requests during the tests
*/
class ThumbnailerTest extends TestCase
{
const WIDTH = 190;
const HEIGHT = 210;
/**
* @var Thumbnailer;
*/
protected $thumbnailer;
/**
* @var ConfigManager
*/
protected $conf;
public function setUp()
{
$this->conf = new ConfigManager('tests/utils/config/configJson');
$this->conf->set('thumbnails.mode', Thumbnailer::MODE_ALL);
$this->conf->set('thumbnails.width', self::WIDTH);
$this->conf->set('thumbnails.height', self::HEIGHT);
$this->conf->set('dev.debug', true);
$this->thumbnailer = new Thumbnailer($this->conf);
// cache files in the sandbox
WTConfigManager::addFile('tests/utils/config/wt.json');
}
public function tearDown()
{
$this->rrmdirContent('sandbox/');
}
/**
* Test a thumbnail with a custom size in 'all' mode.
*/
public function testThumbnailAllValid()
{
$thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
$this->assertNotFalse($thumb);
$image = imagecreatefromstring(file_get_contents($thumb));
$this->assertEquals(self::WIDTH, imagesx($image));
$this->assertEquals(self::HEIGHT, imagesy($image));
}
/**
* Test a thumbnail with a custom size in 'common' mode.
*/
public function testThumbnailCommonValid()
{
$this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
$thumb = $this->thumbnailer->get('https://imgur.com/jlFgGpe');
$this->assertNotFalse($thumb);
$image = imagecreatefromstring(file_get_contents($thumb));
$this->assertEquals(self::WIDTH, imagesx($image));
$this->assertEquals(self::HEIGHT, imagesy($image));
}
/**
* Test a thumbnail in 'common' mode which isn't include in common websites.
*/
public function testThumbnailCommonInvalid()
{
$this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
$thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
$this->assertFalse($thumb);
}
/**
* Test a thumbnail that can't be retrieved.
*/
public function testThumbnailNotValid()
{
$oldlog = ini_get('error_log');
ini_set('error_log', '/dev/null');
$thumbnailer = new Thumbnailer(new ConfigManager());
$thumb = $thumbnailer->get('nope');
$this->assertFalse($thumb);
ini_set('error_log', $oldlog);
}
protected function rrmdirContent($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object))
$this->rrmdirContent($dir."/".$object);
else
unlink($dir."/".$object);
}
}
}
}
}

View file

@ -2,6 +2,7 @@
use Shaarli\Config\ConfigJson;
use Shaarli\Config\ConfigManager;
use Shaarli\Config\ConfigPhp;
use Shaarli\Thumbnailer;
require_once 'tests/Updater/DummyUpdater.php';
require_once 'inc/rain.tpl.class.php';
@ -20,7 +21,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
/**
* @var string Config file path (without extension).
*/
protected static $configFile = 'tests/utils/config/configJson';
protected static $configFile = 'sandbox/config';
/**
* @var ConfigManager
@ -32,6 +33,7 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
*/
public function setUp()
{
copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php');
$this->conf = new ConfigManager(self::$configFile);
}
@ -684,4 +686,50 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
$this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
$this->assertEquals(3, $this->conf->get('general.download_timeout'));
}
/**
* Test updateMethodWebThumbnailer with thumbnails enabled.
*/
public function testUpdateMethodWebThumbnailerEnabled()
{
$this->conf->remove('thumbnails');
$this->conf->set('thumbnail.enable_thumbnails', true);
$updater = new Updater([], [], $this->conf, true, $_SESSION);
$this->assertTrue($updater->updateMethodWebThumbnailer());
$this->assertFalse($this->conf->exists('thumbnail'));
$this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode'));
$this->assertEquals(125, $this->conf->get('thumbnails.width'));
$this->assertEquals(90, $this->conf->get('thumbnails.height'));
$this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
}
/**
* Test updateMethodWebThumbnailer with thumbnails disabled.
*/
public function testUpdateMethodWebThumbnailerDisabled()
{
$this->conf->remove('thumbnails');
$this->conf->set('thumbnail.enable_thumbnails', false);
$updater = new Updater([], [], $this->conf, true, $_SESSION);
$this->assertTrue($updater->updateMethodWebThumbnailer());
$this->assertFalse($this->conf->exists('thumbnail'));
$this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode'));
$this->assertEquals(125, $this->conf->get('thumbnails.width'));
$this->assertEquals(90, $this->conf->get('thumbnails.height'));
$this->assertTrue(empty($_SESSION['warnings']));
}
/**
* Test updateMethodWebThumbnailer with thumbnails disabled.
*/
public function testUpdateMethodWebThumbnailerNothingToDo()
{
$updater = new Updater([], [], $this->conf, true, $_SESSION);
$this->assertTrue($updater->updateMethodWebThumbnailer());
$this->assertFalse($this->conf->exists('thumbnail'));
$this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode'));
$this->assertEquals(90, $this->conf->get('thumbnails.width'));
$this->assertEquals(53, $this->conf->get('thumbnails.height'));
$this->assertTrue(empty($_SESSION['warnings']));
}
}

View file

@ -81,6 +81,18 @@ class ConfigManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
}
public function testSetDeleteNested()
{
$this->conf->set('foo.bar.key.stuff', 'testSetDeleteNested');
$this->assertTrue($this->conf->exists('foo.bar'));
$this->assertTrue($this->conf->exists('foo.bar.key.stuff'));
$this->assertEquals('testSetDeleteNested', $this->conf->get('foo.bar.key.stuff'));
$this->conf->remove('foo.bar');
$this->assertFalse($this->conf->exists('foo.bar.key.stuff'));
$this->assertFalse($this->conf->exists('foo.bar'));
}
/**
* Set with an empty key.
*
@ -103,6 +115,17 @@ class ConfigManagerTest extends \PHPUnit_Framework_TestCase
$this->conf->set(array('foo' => 'bar'), 'stuff');
}
/**
* Remove with an empty key.
*
* @expectedException \Exception
* @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
*/
public function testRmoveEmptyKey()
{
$this->conf->remove('');
}
/**
* Try to write the config without mandatory parameter (e.g. 'login').
*

View file

@ -1,35 +1,84 @@
<?php /*
{
"credentials": {
"login":"root",
"hash":"hash",
"salt":"salt"
"login": "root",
"hash": "hash",
"salt": "salt"
},
"security": {
"session_protection_disabled":false
"session_protection_disabled": false,
"ban_after": 4,
"ban_duration": 1800,
"open_shaarli": false,
"allowed_protocols": [
"ftp",
"ftps",
"magnet"
]
},
"general": {
"timezone":"Europe\/Paris",
"timezone": "Europe\/Paris",
"title": "Shaarli",
"header_link": "?"
"header_link": "?",
"links_per_page": 20,
"enabled_plugins": [
"qrcode"
],
"default_note_title": "Note: "
},
"privacy": {
"default_private_links":true
"default_private_links": true,
"hide_public_links": false,
"force_login": false,
"hide_timestamps": false,
"remember_user_default": true
},
"redirector": {
"url":"lala"
"url": "lala",
"encode_url": true
},
"config": {
"foo": "bar"
},
"resource": {
"datastore": "tests\/utils\/config\/datastore.php",
"data_dir": "sandbox/",
"raintpl_tpl": "tpl/"
"data_dir": "sandbox\/",
"raintpl_tpl": "tpl\/",
"config": "data\/config.php",
"ban_file": "data\/ipbans.php",
"updates": "data\/updates.txt",
"log": "data\/log.txt",
"update_check": "data\/lastupdatecheck.txt",
"history": "data\/history.php",
"theme": "default",
"raintpl_tmp": "tmp\/",
"thumbnails_cache": "cache",
"page_cache": "pagecache"
},
"plugins": {
"WALLABAG_VERSION": 1
},
"dev": {
"debug": true
},
"updates": {
"check_updates": false,
"check_updates_branch": "stable",
"check_updates_interval": 86400
},
"feed": {
"rss_permalinks": true,
"show_atom": true
},
"translation": {
"language": "auto",
"mode": "php",
"extensions": []
},
"thumbnails": {
"mode": "common",
"width": 90,
"height": 53
}
}
*/ ?>

View file

@ -0,0 +1,12 @@
{
"settings": {
"default": {
"_comment": "infinite cache",
"cache_duration": -1,
"timeout": 10
},
"path": {
"cache": "sandbox/"
}
}
}