PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/filter/tests/src/Functional/FilterFormTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 257 lines | 110 code | 26 blank | 121 comment | 0 complexity | a25f8626387f8ce425bdd059c2cedcd5 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\filter\Functional;
  3. use Drupal\filter\Entity\FilterFormat;
  4. use Drupal\Tests\BrowserTestBase;
  5. /**
  6. * Tests form elements with associated text formats.
  7. *
  8. * @group filter
  9. */
  10. class FilterFormTest extends BrowserTestBase {
  11. /**
  12. * Modules to enable for this test.
  13. *
  14. * @var array
  15. */
  16. protected static $modules = ['filter', 'filter_test'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected $defaultTheme = 'stark';
  21. /**
  22. * An administrative user account that can administer text formats.
  23. *
  24. * @var \Drupal\user\Entity\User
  25. */
  26. protected $adminUser;
  27. /**
  28. * A basic user account that can only access basic HTML text format.
  29. *
  30. * @var \Drupal\user\Entity\User
  31. */
  32. protected $webUser;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function setUp(): void {
  37. parent::setUp();
  38. /** @var \Drupal\filter\FilterFormatInterface $filter_test_format */
  39. $filter_test_format = FilterFormat::load('filter_test');
  40. /** @var \Drupal\filter\FilterFormatInterface $filtered_html_format */
  41. $filtered_html_format = FilterFormat::load('filtered_html');
  42. /** @var \Drupal\filter\FilterFormatInterface $full_html_format */
  43. $full_html_format = FilterFormat::load('full_html');
  44. // Create users.
  45. $this->adminUser = $this->drupalCreateUser([
  46. 'administer filters',
  47. $filtered_html_format->getPermissionName(),
  48. $full_html_format->getPermissionName(),
  49. $filter_test_format->getPermissionName(),
  50. ]);
  51. $this->webUser = $this->drupalCreateUser([
  52. $filtered_html_format->getPermissionName(),
  53. $filter_test_format->getPermissionName(),
  54. ]);
  55. }
  56. /**
  57. * Tests various different configurations of the 'text_format' element.
  58. */
  59. public function testFilterForm() {
  60. $this->doFilterFormTestAsAdmin();
  61. $this->doFilterFormTestAsNonAdmin();
  62. // Ensure that enabling modules which provide filter plugins behaves
  63. // correctly.
  64. // @see https://www.drupal.org/node/2387983
  65. \Drupal::service('module_installer')->install(['filter_test_plugin']);
  66. }
  67. /**
  68. * Tests the behavior of the 'text_format' element as an administrator.
  69. */
  70. protected function doFilterFormTestAsAdmin() {
  71. $this->drupalLogin($this->adminUser);
  72. $this->drupalGet('filter-test/text-format');
  73. // Test a text format element with all formats.
  74. $formats = ['filtered_html', 'full_html', 'filter_test'];
  75. $this->assertEnabledTextarea('edit-all-formats-no-default-value');
  76. // If no default is given, the format with the lowest weight becomes the
  77. // default.
  78. $this->assertOptions('edit-all-formats-no-default-format--2', $formats, 'filtered_html');
  79. $this->assertEnabledTextarea('edit-all-formats-default-value');
  80. // \Drupal\filter_test\Form\FilterTestFormatForm::buildForm() uses
  81. // 'filter_test' as the default value in this case.
  82. $this->assertOptions('edit-all-formats-default-format--2', $formats, 'filter_test');
  83. $this->assertEnabledTextarea('edit-all-formats-default-missing-value');
  84. // If a missing format is set as the default, administrators must select a
  85. // valid replacement format.
  86. $this->assertRequiredSelectAndOptions('edit-all-formats-default-missing-format--2', $formats);
  87. // Test a text format element with a predefined list of formats.
  88. $formats = ['full_html', 'filter_test'];
  89. $this->assertEnabledTextarea('edit-restricted-formats-no-default-value');
  90. $this->assertOptions('edit-restricted-formats-no-default-format--2', $formats, 'full_html');
  91. $this->assertEnabledTextarea('edit-restricted-formats-default-value');
  92. $this->assertOptions('edit-restricted-formats-default-format--2', $formats, 'full_html');
  93. $this->assertEnabledTextarea('edit-restricted-formats-default-missing-value');
  94. $this->assertRequiredSelectAndOptions('edit-restricted-formats-default-missing-format--2', $formats);
  95. $this->assertEnabledTextarea('edit-restricted-formats-default-disallowed-value');
  96. $this->assertRequiredSelectAndOptions('edit-restricted-formats-default-disallowed-format--2', $formats);
  97. // Test a text format element with a fixed format.
  98. $formats = ['filter_test'];
  99. // When there is only a single option there is no point in choosing.
  100. $this->assertEnabledTextarea('edit-single-format-no-default-value');
  101. $this->assertNoSelect('edit-single-format-no-default-format--2');
  102. $this->assertEnabledTextarea('edit-single-format-default-value');
  103. $this->assertNoSelect('edit-single-format-default-format--2');
  104. // If the select has a missing or disallowed format, administrators must
  105. // explicitly choose the format.
  106. $this->assertEnabledTextarea('edit-single-format-default-missing-value');
  107. $this->assertRequiredSelectAndOptions('edit-single-format-default-missing-format--2', $formats);
  108. $this->assertEnabledTextarea('edit-single-format-default-disallowed-value');
  109. $this->assertRequiredSelectAndOptions('edit-single-format-default-disallowed-format--2', $formats);
  110. }
  111. /**
  112. * Tests the behavior of the 'text_format' element as a normal user.
  113. */
  114. protected function doFilterFormTestAsNonAdmin() {
  115. $this->drupalLogin($this->webUser);
  116. $this->drupalGet('filter-test/text-format');
  117. // Test a text format element with all formats. Only formats the user has
  118. // access to are shown.
  119. $formats = ['filtered_html', 'filter_test'];
  120. $this->assertEnabledTextarea('edit-all-formats-no-default-value');
  121. // If no default is given, the format with the lowest weight becomes the
  122. // default. This happens to be 'filtered_html'.
  123. $this->assertOptions('edit-all-formats-no-default-format--2', $formats, 'filtered_html');
  124. $this->assertEnabledTextarea('edit-all-formats-default-value');
  125. // \Drupal\filter_test\Form\FilterTestFormatForm::buildForm() uses
  126. // 'filter_test' as the default value in this case.
  127. $this->assertOptions('edit-all-formats-default-format--2', $formats, 'filter_test');
  128. // If a missing format is given as default, non-admin users are presented
  129. // with a disabled textarea.
  130. $this->assertDisabledTextarea('edit-all-formats-default-missing-value');
  131. // Test a text format element with a predefined list of formats.
  132. $this->assertEnabledTextarea('edit-restricted-formats-no-default-value');
  133. // The user only has access to the 'filter_test' format, so when no default
  134. // is given that is preselected and the text format select is hidden.
  135. $this->assertNoSelect('edit-restricted-formats-no-default-format--2');
  136. // When the format that the user does not have access to is preselected, the
  137. // textarea should be disabled.
  138. $this->assertDisabledTextarea('edit-restricted-formats-default-value');
  139. $this->assertDisabledTextarea('edit-restricted-formats-default-missing-value');
  140. $this->assertDisabledTextarea('edit-restricted-formats-default-disallowed-value');
  141. // Test a text format element with a fixed format.
  142. // When there is only a single option there is no point in choosing.
  143. $this->assertEnabledTextarea('edit-single-format-no-default-value');
  144. $this->assertNoSelect('edit-single-format-no-default-format--2');
  145. $this->assertEnabledTextarea('edit-single-format-default-value');
  146. $this->assertNoSelect('edit-single-format-default-format--2');
  147. // If the select has a missing or disallowed format make sure the textarea
  148. // is disabled.
  149. $this->assertDisabledTextarea('edit-single-format-default-missing-value');
  150. $this->assertDisabledTextarea('edit-single-format-default-disallowed-value');
  151. }
  152. /**
  153. * Makes sure that no select element with the given ID exists on the page.
  154. *
  155. * @param string $id
  156. * The HTML ID of the select element.
  157. *
  158. * @internal
  159. */
  160. protected function assertNoSelect(string $id): void {
  161. $this->assertSession()->elementNotExists('xpath', "//select[@id=$id]");
  162. }
  163. /**
  164. * Asserts that a select element has the correct options.
  165. *
  166. * @param string $id
  167. * The HTML ID of the select element.
  168. * @param array $expected_options
  169. * An array of option values.
  170. * @param string $selected
  171. * The value of the selected option.
  172. *
  173. * @internal
  174. */
  175. protected function assertOptions(string $id, array $expected_options, string $selected): void {
  176. $select = $this->assertSession()->selectExists($id);
  177. $found_options = $select->findAll('css', 'option');
  178. $found_options = array_map(function ($item) {
  179. return $item->getValue();
  180. }, $found_options);
  181. $this->assertEqualsCanonicalizing($expected_options, $found_options);
  182. $this->assertTrue($this->assertSession()->optionExists($id, $selected)->isSelected());
  183. }
  184. /**
  185. * Asserts that there is a select element with the given ID that is required.
  186. *
  187. * @param string $id
  188. * The HTML ID of the select element.
  189. * @param array $options
  190. * An array of option values that are contained in the select element
  191. * besides the "- Select -" option.
  192. *
  193. * @internal
  194. */
  195. protected function assertRequiredSelectAndOptions(string $id, array $options): void {
  196. $select = $this->assertSession()->selectExists($id);
  197. $this->assertSame('required', $select->getAttribute('required'));
  198. // A required select element has a "- Select -" option whose key is an empty
  199. // string.
  200. $options[] = '';
  201. $this->assertOptions($id, $options, '');
  202. }
  203. /**
  204. * Asserts that a textarea with a given ID exists and is not disabled.
  205. *
  206. * @param string $id
  207. * The HTML ID of the textarea.
  208. *
  209. * @internal
  210. */
  211. protected function assertEnabledTextarea(string $id): void {
  212. $textarea = $this->assertSession()->fieldEnabled($id);
  213. $this->assertSame('textarea', $textarea->getTagName());
  214. }
  215. /**
  216. * Asserts that a textarea with a given ID has been disabled from editing.
  217. *
  218. * @param string $id
  219. * The HTML ID of the textarea.
  220. *
  221. * @internal
  222. */
  223. protected function assertDisabledTextarea(string $id): void {
  224. $textarea = $this->assertSession()->fieldDisabled($id);
  225. $this->assertSame('textarea', $textarea->getTagName());
  226. $this->assertSame('This field has been disabled because you do not have sufficient permissions to edit it.', $textarea->getText());
  227. // Make sure the text format select is not shown.
  228. $select_id = str_replace('value', 'format--2', $id);
  229. $this->assertNoSelect($select_id);
  230. }
  231. }