PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/ckeditor/tests/modules/src/Kernel/CKEditorPluginManagerTest.php

https://gitlab.com/reasonat/test8
PHP | 159 lines | 92 code | 18 blank | 49 comment | 0 complexity | 4c6809ea0fba3f89c890dba2a8ae8db4 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\ckeditor\Kernel;
  3. use Drupal\editor\Entity\Editor;
  4. use Drupal\KernelTests\KernelTestBase;
  5. use Drupal\filter\Entity\FilterFormat;
  6. /**
  7. * Tests different ways of enabling CKEditor plugins.
  8. *
  9. * @group ckeditor
  10. */
  11. class CKEditorPluginManagerTest extends KernelTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = array('system', 'user', 'filter', 'editor', 'ckeditor');
  18. /**
  19. * The manager for "CKEditor plugin" plugins.
  20. *
  21. * @var \Drupal\Component\Plugin\PluginManagerInterface
  22. */
  23. protected $manager;
  24. protected function setUp() {
  25. parent::setUp();
  26. // Install the Filter module.
  27. // Create text format, associate CKEditor.
  28. $filtered_html_format = FilterFormat::create(array(
  29. 'format' => 'filtered_html',
  30. 'name' => 'Filtered HTML',
  31. 'weight' => 0,
  32. 'filters' => array(),
  33. ));
  34. $filtered_html_format->save();
  35. $editor = Editor::create([
  36. 'format' => 'filtered_html',
  37. 'editor' => 'ckeditor',
  38. ]);
  39. $editor->save();
  40. }
  41. /**
  42. * Tests the enabling of plugins.
  43. */
  44. function testEnabledPlugins() {
  45. $this->manager = $this->container->get('plugin.manager.ckeditor.plugin');
  46. $editor = Editor::load('filtered_html');
  47. // Case 1: no CKEditor plugins.
  48. $definitions = array_keys($this->manager->getDefinitions());
  49. sort($definitions);
  50. $this->assertIdentical(array('drupalimage', 'drupalimagecaption', 'drupallink', 'internal', 'language', 'stylescombo'), $definitions, 'No CKEditor plugins found besides the built-in ones.');
  51. $enabled_plugins = array(
  52. 'drupalimage' => drupal_get_path('module', 'ckeditor') . '/js/plugins/drupalimage/plugin.js',
  53. 'drupallink' => drupal_get_path('module', 'ckeditor') . '/js/plugins/drupallink/plugin.js',
  54. );
  55. $this->assertIdentical($enabled_plugins, $this->manager->getEnabledPluginFiles($editor), 'Only built-in plugins are enabled.');
  56. $this->assertIdentical(array('internal' => NULL) + $enabled_plugins, $this->manager->getEnabledPluginFiles($editor, TRUE), 'Only the "internal" plugin is enabled.');
  57. // Enable the CKEditor Test module, which has the Llama plugin (plus four
  58. // variations of it, to cover all possible ways a plugin can be enabled) and
  59. // clear the editor manager's cache so it is picked up.
  60. $this->enableModules(array('ckeditor_test'));
  61. $this->manager = $this->container->get('plugin.manager.ckeditor.plugin');
  62. $this->manager->clearCachedDefinitions();
  63. // Case 2: CKEditor plugins are available.
  64. $plugin_ids = array_keys($this->manager->getDefinitions());
  65. sort($plugin_ids);
  66. $this->assertIdentical(array('drupalimage', 'drupalimagecaption', 'drupallink', 'internal', 'language', 'llama', 'llama_button', 'llama_contextual', 'llama_contextual_and_button', 'llama_css', 'stylescombo'), $plugin_ids, 'Additional CKEditor plugins found.');
  67. $this->assertIdentical($enabled_plugins, $this->manager->getEnabledPluginFiles($editor), 'Only the internal plugins are enabled.');
  68. $this->assertIdentical(array('internal' => NULL) + $enabled_plugins, $this->manager->getEnabledPluginFiles($editor, TRUE), 'Only the "internal" plugin is enabled.');
  69. // Case 3: enable each of the newly available plugins, if possible:
  70. // a. Llama: cannot be enabled, since it does not implement
  71. // CKEditorPluginContextualInterface nor CKEditorPluginButtonsInterface.
  72. // b. LlamaContextual: enabled by adding the 'Strike' button, which is
  73. // part of another plugin!
  74. // c. LlamaButton: automatically enabled by adding its 'Llama' button.
  75. // d. LlamaContextualAndButton: enabled by either b or c.
  76. // e. LlamaCSS: automatically enabled by add its 'LlamaCSS' button.
  77. // Below, we will first enable the "Llama" button, which will cause the
  78. // LlamaButton and LlamaContextualAndButton plugins to be enabled. Then we
  79. // will remove the "Llama" button and add the "Strike" button, which will
  80. // cause the LlamaContextual and LlamaContextualAndButton plugins to be
  81. // enabled. Then we will add the "Strike" button back again, which would
  82. // cause LlamaButton, LlamaContextual and LlamaContextualAndButton to be
  83. // enabled. Finally, we will add the "LlamaCSS" button which would cause
  84. // all four plugins to be enabled.
  85. $settings = $editor->getSettings();
  86. $original_toolbar = $settings['toolbar'];
  87. $settings['toolbar']['rows'][0][0]['items'][] = 'Llama';
  88. $editor->setSettings($settings);
  89. $editor->save();
  90. $file = array();
  91. $file['b'] = drupal_get_path('module', 'ckeditor_test') . '/js/llama_button.js';
  92. $file['c'] = drupal_get_path('module', 'ckeditor_test') . '/js/llama_contextual.js';
  93. $file['cb'] = drupal_get_path('module', 'ckeditor_test') . '/js/llama_contextual_and_button.js';
  94. $file['css'] = drupal_get_path('module', 'ckeditor_test') . '/js/llama_css.js';
  95. $expected = $enabled_plugins + array('llama_button' => $file['b'], 'llama_contextual_and_button' => $file['cb']);
  96. $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LlamaButton and LlamaContextualAndButton plugins are enabled.');
  97. $this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LlamaButton and LlamaContextualAndButton plugins are enabled.');
  98. $settings['toolbar'] = $original_toolbar;
  99. $settings['toolbar']['rows'][0][0]['items'][] = 'Strike';
  100. $editor->setSettings($settings);
  101. $editor->save();
  102. $expected = $enabled_plugins + array('llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb']);
  103. $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LLamaContextual and LlamaContextualAndButton plugins are enabled.');
  104. $this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LlamaContextual and LlamaContextualAndButton plugins are enabled.');
  105. $settings['toolbar']['rows'][0][0]['items'][] = 'Llama';
  106. $editor->setSettings($settings);
  107. $editor->save();
  108. $expected = $enabled_plugins + array('llama_button' => $file['b'], 'llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb']);
  109. $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LlamaButton, LlamaContextual and LlamaContextualAndButton plugins are enabled.');
  110. $this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LLamaButton, LlamaContextual and LlamaContextualAndButton plugins are enabled.');
  111. $settings['toolbar']['rows'][0][0]['items'][] = 'LlamaCSS';
  112. $editor->setSettings($settings);
  113. $editor->save();
  114. $expected = $enabled_plugins + array('llama_button' => $file['b'], 'llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb'], 'llama_css' => $file['css']);
  115. $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LlamaButton, LlamaContextual, LlamaContextualAndButton and LlamaCSS plugins are enabled.');
  116. $this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LLamaButton, LlamaContextual, LlamaContextualAndButton and LlamaCSS plugins are enabled.');
  117. }
  118. /**
  119. * Tests the iframe instance CSS files of plugins.
  120. */
  121. function testCssFiles() {
  122. $this->manager = $this->container->get('plugin.manager.ckeditor.plugin');
  123. $editor = Editor::load('filtered_html');
  124. // Case 1: no CKEditor iframe instance CSS file.
  125. $this->assertIdentical(array(), $this->manager->getCssFiles($editor), 'No iframe instance CSS file found.');
  126. // Enable the CKEditor Test module, which has the LlamaCss plugin and
  127. // clear the editor manager's cache so it is picked up.
  128. $this->enableModules(array('ckeditor_test'));
  129. $this->manager = $this->container->get('plugin.manager.ckeditor.plugin');
  130. $settings = $editor->getSettings();
  131. // LlamaCss: automatically enabled by adding its 'LlamaCSS' button.
  132. $settings['toolbar']['rows'][0][0]['items'][] = 'LlamaCSS';
  133. $editor->setSettings($settings);
  134. $editor->save();
  135. // Case 2: CKEditor iframe instance CSS file.
  136. $expected = array(
  137. 'llama_css' => array(drupal_get_path('module', 'ckeditor_test') . '/css/llama.css')
  138. );
  139. $this->assertIdentical($expected, $this->manager->getCssFiles($editor), 'Iframe instance CSS file found.');
  140. }
  141. }