Skip to content

Commit

Permalink
Merge pull request #1285 from NatLibFi/issue1148-order-of-plugin-rend…
Browse files Browse the repository at this point in the history
…ering

Issue1148 order of plugin rendering
  • Loading branch information
osma authored Mar 4, 2022
2 parents 2a61720 + 9958dbb commit 958e067
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 20 deletions.
42 changes: 38 additions & 4 deletions model/PluginRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class PluginRegister {

public function __construct($requestedPlugins=array()) {
$this->requestedPlugins = $requestedPlugins;
$this->pluginOrder = array();
foreach ($this->requestedPlugins as $index => $value) {
$this->pluginOrder[$value] = $index;
}
}

/**
Expand All @@ -24,6 +28,16 @@ protected function getPlugins()
return $plugins;
}

/**
* Sort plugins by order defined in class constructor
* @return array
*/
private function sortPlugins($plugins) {
$sortedPlugins = array();
$sortedPlugins = array_replace($this->pluginOrder, $plugins);
return $sortedPlugins;
}

/**
* Returns the plugin configurations found from plugin folders
* inside the plugins folder filtered by filetype.
Expand All @@ -33,6 +47,7 @@ protected function getPlugins()
*/
private function filterPlugins($type, $raw=false) {
$plugins = $this->getPlugins();
$plugins = $this->sortPlugins($plugins);
$ret = array();
if (!empty($plugins)) {
foreach ($plugins as $name => $files) {
Expand Down Expand Up @@ -130,21 +145,40 @@ public function getTemplates($names=null) {
}

/**
* Returns an array of javascript function names to call when loading pages
* Returns an array of plugin callback function names
* @param array $names the plugin name strings (foldernames) in an array
* @return array
*/
public function getPluginCallbacks($names=null) {
if ($names) {
$names = array_merge($this->requestedPlugins, $names);
return $this->filterPluginsByName('callback', $names);
}
return $this->filterPluginsByName('callback', $this->requestedPlugins);
}

/**
* Returns a sorted array of javascript function names to call when loading pages
* in order configured with skosmos:vocabularyPlugins
* @return array
*/
public function getCallbacks() {
$ret = array();
$plugins = $this->filterPluginsByName('callback', $this->requestedPlugins);
$sortedCallbacks = array();
$plugins = $this->getPluginCallbacks($this->requestedPlugins);
foreach ($plugins as $callbacks) {
foreach ($callbacks as $callback) {
$split = explode('/', $callback);
$ret[] = $split[2];
$sortedCallbacks[$split[1]] = $split[2];
}
}
$sortedCallbacks = array_replace($this->pluginOrder, $sortedCallbacks);
foreach ($sortedCallbacks as $callback) {
$ret[] = $callback;
}
return $ret;
}

/**
* Returns an array that is flattened from its possibly multidimensional form
* copied from https://stackoverflow.com/a/1320156
Expand Down
8 changes: 7 additions & 1 deletion model/VocabularyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function __construct($resource, $globalPlugins=array())
{
$this->resource = $resource;
$this->globalPlugins = $globalPlugins;
$this->setParameterizedPlugins();
$this->setPropertyLabelOverrides();
$pluginArray = $this->getPluginArray();
$this->pluginRegister = new PluginRegister($pluginArray);
Expand All @@ -45,6 +44,7 @@ public function __construct($resource, $globalPlugins=array())
*/
public function getPluginArray() : array
{
$this->setParameterizedPlugins();
$pluginArray = array();
$vocabularyPlugins = $this->resource->getResource('skosmos:vocabularyPlugins');
if (!$vocabularyPlugins instanceof EasyRdf\Collection) {
Expand All @@ -62,6 +62,12 @@ public function getPluginArray() : array
}
$pluginArray = array_merge($pluginArray, $this->globalPlugins);

$paramPlugins = $this->resource->allResources('skosmos:useParamPlugin');
if ($paramPlugins) {
foreach ($paramPlugins as $plugin) {
$pluginArray[] = $plugin->getLiteral('skosmos:usePlugin')->getValue();
}
}
$plugins = $this->resource->allLiterals('skosmos:usePlugin');
if ($plugins) {
foreach ($plugins as $pluginlit) {
Expand Down
90 changes: 82 additions & 8 deletions tests/PluginRegisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,44 @@ class PluginRegisterTest extends PHPUnit\Framework\TestCase

protected function setUp() : void
{
$this->mockpr = $this->getMockBuilder('PluginRegister')->setConstructorArgs(array(array('global-plugin')))->setMethods(array('getPlugins'))->getMock();
$stubplugs = array ('test-plugin' => array ( 'js' => array ( 0 => 'first.js', 1 => 'second.min.js', ), 'css' => array ( 0 => 'stylesheet.css', ), 'templates' => array ( 0 => 'template.html', ), ), 'only-css' => array ( 'css' => array ( 0 => 'super.css')), 'global-plugin' => array('js' => array('everywhere.js')));
$this->mockpr->method('getPlugins')->will($this->returnValue($stubplugs));
$this->mockpr = $this->getMockBuilder('PluginRegister')->setConstructorArgs(array(array('test-plugin2',
'global-plugin-Bravo',
'imaginary-plugin',
'test-plugin1',
'global-plugin-alpha',
'global-plugin-charlie',
'test-plugin3'
)))
->setMethods(['getPlugins'])
->getMock();
$this->stubplugs = array ('imaginary-plugin' => array ( 'js' => array ( 0 => 'imaginaryPlugin.js', ),
'css' => array ( 0 => 'stylesheet.css', ),
'templates' => array ( 0 => 'template.html', ),
'callback' => array ( 0 => 'imaginaryPlugin')
),
'test-plugin1' => array ( 'js' => array ( 0 => 'plugin1.js', 1 => 'second.min.js'),
'css' => array ( 0 => 'stylesheet.css', ),
'templates' => array ( 0 => 'template.html', ),
'callback' => array ( 0 => 'callplugin1')
),
'test-plugin2' => array ( 'js' => array ( 0 => 'plugin2.js', ),
'css' => array ( 0 => 'stylesheet.css', ),
'templates' => array ( 0 => 'template.html', ),
'callback' => array ( 0 => 'callplugin2')
),
'test-plugin3' => array ( 'js' => array ( 0 => 'plugin3.js', ),
'css' => array ( 0 => 'stylesheet.css', ),
'templates' => array ( 0 => 'template.html', ),
'callback' => array ( 0 => 'callplugin3')
),
'only-css' => array ( 'css' => array ( 0 => 'super.css')),
'global-plugin-alpha' => array('js' => array('alpha.js'),
'callback' => array ( 0 => 'alpha')),
'global-plugin-Bravo' => array('js' => array('Bravo.js'),
'callback' => array ( 0 => 'bravo')),
'global-plugin-charlie' => array('js' => array('charlie.js'),
'callback' => array ( 0 => 'charlie')));
$this->mockpr->method('getPlugins')->will($this->returnValue($this->stubplugs));
$this->model = new Model(new GlobalConfig('/../tests/testconfig.ttl'));
$this->vocab = $this->model->getVocabulary('test');
}
Expand Down Expand Up @@ -39,21 +74,45 @@ public function testGetPluginsJS()
* @covers PluginRegister::getPluginsJS
* @covers PluginRegister::filterPlugins
* @covers PluginRegister::filterPluginsByName
* @covers PluginRegister::sortPlugins
*/
public function testGetPluginsJSInOrder()
{
$this->assertEquals(['test-plugin2',
'global-plugin-Bravo',
'imaginary-plugin',
'test-plugin1',
'global-plugin-alpha',
'global-plugin-charlie',
'test-plugin3'],
array_keys($this->mockpr->getPluginsJS()));

}

/**
* @covers PluginRegister::getPlugins
* @covers PluginRegister::getPluginsJS
* @covers PluginRegister::filterPlugins
* @covers PluginRegister::filterPluginsByName
* @covers PluginRegister::sortPlugins
*/
public function testGetPluginsJSWithName()
{
$this->assertEquals(array('global-plugin' => array('plugins/global-plugin/everywhere.js'), 'test-plugin' => array('plugins/test-plugin/first.js', 'plugins/test-plugin/second.min.js')), $this->mockpr->getPluginsJS(array('test-plugin')));
$this->assertEquals(array('plugins/test-plugin1/plugin1.js', 'plugins/test-plugin1/second.min.js'),
$this->mockpr->getPluginsJS()['test-plugin1']);
}

/**
* @covers PluginRegister::getPlugins
* @covers PluginRegister::getPluginsJS
* @covers PluginRegister::filterPlugins
* @covers PluginRegister::filterPluginsByName
* @covers PluginRegister::sortPlugins
*/
public function testGetPluginsJSWithGlobalPlugin()
{
$this->assertEquals(array('global-plugin' => array('plugins/global-plugin/everywhere.js')), $this->mockpr->getPluginsJS());
$this->assertEquals(array('plugins/global-plugin-alpha/alpha.js'),
$this->mockpr->getPluginsJS()['global-plugin-alpha']);
}

/**
Expand All @@ -69,11 +128,25 @@ public function testGetPluginsCSS()
* @covers PluginRegister::getPluginsCSS
* @covers PluginRegister::filterPlugins
* @covers PluginRegister::filterPluginsByName
* @covers PluginRegister::sortPlugins
*/
public function testGetPluginsCSSWithName()
{
$this->assertEquals(array('plugins/test-plugin1/stylesheet.css'),
$this->mockpr->getPluginsCSS()['test-plugin1']);
}

/**
* @covers PluginRegister::getPluginCallbacks
* @covers PluginRegister::filterPlugins
* @covers PluginRegister::filterPluginsByName
* @covers PluginRegister::sortPlugins
*/
public function testGetPluginCallbacks()
{
$plugins = new PluginRegister();
$this->assertEquals(array('test-plugin' => array('plugins/test-plugin/stylesheet.css')), $this->mockpr->getPluginsCSS(array('test-plugin')));
$this->assertEquals(array('plugins/test-plugin1/callplugin1'),
$this->mockpr->getPluginCallbacks()['test-plugin1']);
}

/**
Expand All @@ -99,8 +172,9 @@ public function testGetTemplates()
*/
public function testGetCallbacks()
{
$plugins = new PluginRegister();
$this->assertEquals(array(), $plugins->getCallbacks());
$this->assertEquals(array('callplugin2', 'bravo', 'imaginaryPlugin', 'callplugin1', 'alpha', 'charlie', 'callplugin3'),
$this->mockpr->getCallbacks()
);
}

}
8 changes: 4 additions & 4 deletions tests/VocabularyConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public function testGetVocabularyUseModifiedDate() {
* @covers VocabularyConfig::getPluginParameters
*/
public function testGetPluginParameters() {
$vocab = $this->model->getVocabulary('paramPluginTest');
$vocab = $this->model->getVocabulary('paramPluginOrderTest');
$params = $vocab->getConfig()->getPluginParameters();
$this->assertEquals(array('imaginaryPlugin' => array('poem_fi' => "Roses are red", 'poem' => "Violets are blue", 'color' => "#800000")), $params);
}
Expand All @@ -550,16 +550,16 @@ public function testGetPluginParameters() {
* @covers VocabularyConfig::getPluginArray
*/
public function testGetOrderedPlugins() {
$vocab = $this->model->getVocabulary('paramPluginTest');
$vocab = $this->model->getVocabulary('paramPluginOrderTest');
$plugins = $vocab->getConfig()->getPluginArray();
$this->assertEquals(["plugin2", "Bravo", "imaginaryPlugin", "plugin1", "alpha", "charlie", "plugin3"], $plugins);
$this->assertEquals(["plugin2", "Bravo", "plugin1", "alpha", "charlie", "imaginaryPlugin", "plugin3"], $plugins);
}

/**
* @covers VocabularyConfig::getPluginArray
*/
public function testGetUnorderedVocabularyPlugins() {
$vocab = $this->model->getVocabulary('paramPluginOrderTest');
$vocab = $this->model->getVocabulary('paramPluginTest');
$plugins = $vocab->getConfig()->getPluginArray();
$arrayElements = ["plugin2", "Bravo", "imaginaryPlugin", "plugin1", "alpha", "charlie", "plugin3"];
$this->assertEquals(sort($arrayElements), sort($plugins));
Expand Down
6 changes: 3 additions & 3 deletions tests/testconfig.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@
skosmos:sparqlGraph <http://www.skosmos.skos/test-notation-sort/>;
skosmos:mainConceptScheme test:notationMainConceptScheme .

:paramPluginOrderTest a skosmos:Vocabulary, void:Dataset ;
:paramPluginTest a skosmos:Vocabulary, void:Dataset ;
dc:title "Test plugin order"@en ;
void:dataDump <http://skosmos.skos/dump/test/> ;
void:uriSpace "http://www.skosmos.skos/test/";
Expand All @@ -371,7 +371,7 @@
skosmos:usePlugin "plugin3" ;
skosmos:useParamPlugin :parameterizedPlugin .

:paramPluginTest a skosmos:Vocabulary, void:Dataset ;
:paramPluginOrderTest a skosmos:Vocabulary, void:Dataset ;
dc:title "Test plugin parameters"@en ;
dc:subject :cat_science ;
dc:type mdrtype:ONTOLOGY ;
Expand All @@ -383,7 +383,7 @@
skosmos:feedbackRecipient "[email protected]";
skosmos:groupClass skos:Collection;
skosmos:language "en";
skosmos:vocabularyPlugins ("plugin2" "Bravo" :parameterizedPlugin "plugin1");
skosmos:vocabularyPlugins ("plugin2" "Bravo" "plugin1");
skosmos:usePlugin "plugin1" ;
skosmos:usePlugin "plugin3" ;
skosmos:useParamPlugin :parameterizedPlugin ;
Expand Down
2 changes: 2 additions & 0 deletions view/concept-shared.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<p class="language-alert">{% trans %}There is no term for this concept in this language{% endtrans %}</p>
</div>
{% endif %}
<div id="concept-info-before"></div>
<div class="concept-info{% if concept.deprecated %} deprecated-concept{% endif %}">
<div class="concept-main">
{% if bread_crumbs is defined %}
Expand Down Expand Up @@ -208,6 +209,7 @@
>
</div>
</div>
<div id="concept-info-after"></div>
{% endfor %}

{% else %}
Expand Down

0 comments on commit 958e067

Please sign in to comment.