Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call plugin callbacks in the order they are configured #1285

Merged
merged 9 commits into from
Mar 4, 2022
31 changes: 27 additions & 4 deletions model/PluginRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,44 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests don't cover the case of $names being passed. I think it should be easy to modify the test for getPluginCallbacks. I will suggest below...

$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();
$order = array();
foreach ($this->requestedPlugins as $index => $value) {
$order[$value] = $index;
}
$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($order, $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
66 changes: 58 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 @@ -42,7 +77,8 @@ public function testGetPluginsJS()
*/
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($this->mockpr->getPluginsJS()['test-plugin1'],
array('plugins/test-plugin1/plugin1.js', 'plugins/test-plugin1/second.min.js'));
}

/**
Expand All @@ -53,7 +89,8 @@ public function testGetPluginsJSWithName()
*/
public function testGetPluginsJSWithGlobalPlugin()
{
$this->assertEquals(array('global-plugin' => array('plugins/global-plugin/everywhere.js')), $this->mockpr->getPluginsJS());
$this->assertEquals($this->mockpr->getPluginsJS()['global-plugin-alpha'],
array('plugins/global-plugin-alpha/alpha.js'));
}

/**
Expand All @@ -71,9 +108,21 @@ public function testGetPluginsCSS()
* @covers PluginRegister::filterPluginsByName
*/
public function testGetPluginsCSSWithName()
{
$this->assertEquals($this->mockpr->getPluginsCSS()['test-plugin1'],
array('plugins/test-plugin1/stylesheet.css'));
}

/**
* @covers PluginRegister::getPluginCallbacks
* @covers PluginRegister::filterPlugins
* @covers PluginRegister::filterPluginsByName
*/
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($this->mockpr->getPluginCallbacks()['test-plugin1'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here passing a $names parameter to getPluginCallbacks would increase test coverage.

array('plugins/test-plugin1/callplugin1'));
}

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

}
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 "developer@vocabulary.org";
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