mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/16944] Add tests for iconify bundler
PHPBB3-16944
This commit is contained in:
parent
b4b7199ed5
commit
c98778dbf6
2 changed files with 166 additions and 25 deletions
|
@ -47,22 +47,26 @@ class iconify_bundler
|
||||||
$organized_icons = $this->organize_icons_list();
|
$organized_icons = $this->organize_icons_list();
|
||||||
|
|
||||||
$output = $this->load_icons_data($organized_icons);
|
$output = $this->load_icons_data($organized_icons);
|
||||||
|
if (!$output)
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
$output = '(function() {
|
$output = '(function() {
|
||||||
function add(data) {
|
function add(data) {
|
||||||
try {
|
try {
|
||||||
if (typeof self.Iconify === \'object\' && self.Iconify.addCollection) {
|
if (typeof self.Iconify === \'object\' && self.Iconify.addCollection) {
|
||||||
self.Iconify.addCollection(data);
|
self.Iconify.addCollection(data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (typeof self.IconifyPreload === \'undefined\') {
|
if (typeof self.IconifyPreload === \'undefined\') {
|
||||||
self.IconifyPreload = [];
|
self.IconifyPreload = [];
|
||||||
}
|
}
|
||||||
self.IconifyPreload.push(data);
|
self.IconifyPreload.push(data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
' . $output . '
|
' . $output . '
|
||||||
})();' . "\n";
|
})();' . "\n";
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
|
@ -111,7 +115,7 @@ class iconify_bundler
|
||||||
{
|
{
|
||||||
// Split icon to prefix and name
|
// Split icon to prefix and name
|
||||||
$icon = $this->name_to_icon($icon_name);
|
$icon = $this->name_to_icon($icon_name);
|
||||||
if ($icon === null || $icon['provider'] !== '')
|
if ($icon === null)
|
||||||
{
|
{
|
||||||
// Invalid name or icon name does not have provider
|
// Invalid name or icon name does not have provider
|
||||||
if ($this->log)
|
if ($this->log)
|
||||||
|
@ -216,24 +220,20 @@ class iconify_bundler
|
||||||
{
|
{
|
||||||
// Load icon set
|
// Load icon set
|
||||||
$collection = new Collection($prefix);
|
$collection = new Collection($prefix);
|
||||||
if (!$collection->loadIconifyCollection($prefix))
|
$collection_file = Collection::findIconifyCollection($prefix);
|
||||||
|
if (!file_exists($collection_file) || !$collection->loadFromFile($collection_file))
|
||||||
{
|
{
|
||||||
if ($this->log)
|
$this->log?->add('critical', ANONYMOUS, '', 'LOG_ICON_COLLECTION_INVALID', false, [$prefix]);
|
||||||
{
|
|
||||||
$this->log->add('critical', ANONYMOUS, '', 'LOG_ICON_COLLECTION_INVALID', false, [$prefix]);
|
|
||||||
}
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure all icons exist
|
// Make sure all icons exist
|
||||||
foreach ($iconsList as $name)
|
foreach ($iconsList as $key => $name)
|
||||||
{
|
{
|
||||||
if (!$collection->iconExists($name))
|
if (!$collection->iconExists($name))
|
||||||
{
|
{
|
||||||
if ($this->log)
|
$this->log?->add('critical', ANONYMOUS, '', 'LOG_ICON_INVALID', false, [$prefix . ':' . $name]);
|
||||||
{
|
unset($iconsList[$key]);
|
||||||
$this->log->add('critical', ANONYMOUS, '', 'LOG_ICON_INVALID', false, [$prefix . ':' . $name]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
141
tests/assets/iconify_bundler_tests.php
Normal file
141
tests/assets/iconify_bundler_tests.php
Normal file
|
@ -0,0 +1,141 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* This file is part of the phpBB Forum Software package.
|
||||||
|
*
|
||||||
|
* @copyright (c) phpBB Limited <https://www.phpbb.com>
|
||||||
|
* @license GNU General Public License, version 2 (GPL-2.0)
|
||||||
|
*
|
||||||
|
* For full copyright and license information, please see
|
||||||
|
* the docs/CREDITS.txt file.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace phpbb\tests\unit\assets;
|
||||||
|
|
||||||
|
class iconify_bundler_tests extends \phpbb_test_case
|
||||||
|
{
|
||||||
|
/** @var array Log content */
|
||||||
|
protected $log_content = [];
|
||||||
|
|
||||||
|
/** @var \phpbb\assets\iconify_bundler */
|
||||||
|
protected $bundler;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$log = $this->getMockBuilder('\phpbb\log\dummy')
|
||||||
|
->onlyMethods(['add'])
|
||||||
|
->getMock();
|
||||||
|
$log->method('add')
|
||||||
|
->willReturnCallback(function ($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = array()) {
|
||||||
|
$this->log_content[] = $log_operation;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->bundler = new \phpbb\assets\iconify_bundler($log);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function data_test_generate_bundle()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
['fa:address-card-o'],
|
||||||
|
['"prefix":"fa"', '"address-card-o"'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['fa:address-card-o', 'fa-regular:credit-card'],
|
||||||
|
['"prefix":"fa"', '"address-card-o"', '"prefix":"fa-regular"', '"credit-card"'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['fa:address-card-o', 'fa:foo-bar'],
|
||||||
|
['"prefix":"fa"', '"address-card-o"'],
|
||||||
|
['LOG_ICON_INVALID'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['fa:address-card-o', 'fa-regular:credit-card', 'fa-regular:credit-card'],
|
||||||
|
['"prefix":"fa"', '"address-card-o"', '"prefix":"fa-regular"', '"credit-card"'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['fa:address-card-o', 'fa-regular:credit-card', 'fa-regular:angry'],
|
||||||
|
['"prefix":"fa"', '"address-card-o"', '"prefix":"fa-regular"', '"credit-card"', '"angry"'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['fa:address-card-o', 'fa:bell', 'fa-regular:credit-card', 'fa-regular:angry'],
|
||||||
|
['"prefix":"fa"', '"address-card-o"', '"bell"', '"prefix":"fa-regular"', '"credit-card"', '"angry"'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['@test'],
|
||||||
|
[],
|
||||||
|
['LOG_ICON_INVALID'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['fa:address-foo-o'],
|
||||||
|
['"prefix":"fa"', '"icons":[]'],
|
||||||
|
['LOG_ICON_INVALID'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['foo:bar'],
|
||||||
|
[],
|
||||||
|
['LOG_ICON_COLLECTION_INVALID']
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['@iconify:fa:address-card-o'],
|
||||||
|
['"prefix":"fa"', '"address-card-o"'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['@iconify:someother:fa:address-card-o'],
|
||||||
|
[],
|
||||||
|
['LOG_ICON_INVALID'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['iconify:fa:address-card-o'],
|
||||||
|
['"prefix":"fa"', '"address-card-o"'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['iconify:fa:fa:address-card-o'],
|
||||||
|
[],
|
||||||
|
['LOG_ICON_INVALID'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['test'],
|
||||||
|
[],
|
||||||
|
['LOG_ICON_INVALID'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[''],
|
||||||
|
[],
|
||||||
|
['LOG_ICON_INVALID'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
['fa-address-card-o'],
|
||||||
|
['"prefix":"fa"', '"address-card-o"'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider data_test_generate_bundle
|
||||||
|
*/
|
||||||
|
public function test_generate_bundle($icons, $expected, $log_content = [])
|
||||||
|
{
|
||||||
|
$this->bundler->add_icons($icons);
|
||||||
|
$bundle = $this->bundler->run();
|
||||||
|
foreach ($expected as $expected_part)
|
||||||
|
{
|
||||||
|
$this->assertStringContainsString($expected_part, $bundle, 'Failed asserting that generated bundle contains ' . $expected_part);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!count($expected))
|
||||||
|
{
|
||||||
|
$this->assertEquals($bundle, '', 'Failed asserting that generated bundle is empty');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($log_content))
|
||||||
|
{
|
||||||
|
$this->assertEquals($this->log_content, $log_content, 'Failed asserting that log content is correct');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$this->assertEmpty($this->log_content, 'Failed asserting that log content is empty');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue