PluginManagerTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. use Shaarli\Config\ConfigManager;
  3. /**
  4. * Plugin Manager tests
  5. */
  6. require_once 'application/PluginManager.php';
  7. /**
  8. * Unit tests for Plugins
  9. */
  10. class PluginManagerTest extends PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * Path to tests plugin.
  14. * @var string $pluginPath
  15. */
  16. private static $pluginPath = 'tests/plugins';
  17. /**
  18. * Test plugin.
  19. * @var string $pluginName
  20. */
  21. private static $pluginName = 'test';
  22. /**
  23. * @var PluginManager $pluginManager Plugin Mananger instance.
  24. */
  25. protected $pluginManager;
  26. public function setUp()
  27. {
  28. $conf = new ConfigManager('');
  29. $this->pluginManager = new PluginManager($conf);
  30. }
  31. /**
  32. * Test plugin loading and hook execution.
  33. *
  34. * @return void
  35. */
  36. public function testPlugin()
  37. {
  38. PluginManager::$PLUGINS_PATH = self::$pluginPath;
  39. $this->pluginManager->load(array(self::$pluginName));
  40. $this->assertTrue(function_exists('hook_test_random'));
  41. $data = array(0 => 'woot');
  42. $this->pluginManager->executeHooks('random', $data);
  43. $this->assertEquals('woot', $data[1]);
  44. $data = array(0 => 'woot');
  45. $this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
  46. $this->assertEquals('page test', $data[1]);
  47. $data = array(0 => 'woot');
  48. $this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
  49. $this->assertEquals('loggedin', $data[1]);
  50. }
  51. /**
  52. * Test missing plugin loading.
  53. *
  54. * @return void
  55. */
  56. public function testPluginNotFound()
  57. {
  58. $this->pluginManager->load(array());
  59. $this->pluginManager->load(array('nope', 'renope'));
  60. }
  61. /**
  62. * Test plugin metadata loading.
  63. */
  64. public function testGetPluginsMeta()
  65. {
  66. PluginManager::$PLUGINS_PATH = self::$pluginPath;
  67. $this->pluginManager->load(array(self::$pluginName));
  68. $expectedParameters = array(
  69. 'pop' => array(
  70. 'value' => '',
  71. 'desc' => 'pop description',
  72. ),
  73. 'hip' => array(
  74. 'value' => '',
  75. 'desc' => '',
  76. ),
  77. );
  78. $meta = $this->pluginManager->getPluginsMeta();
  79. $this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
  80. $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
  81. }
  82. }