X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Ffilter%2Ftests%2Fsrc%2FFunctional%2FFilterAdminTest.php;fp=web%2Fcore%2Fmodules%2Ffilter%2Ftests%2Fsrc%2FFunctional%2FFilterAdminTest.php;h=21431e7969a341f0f284d352f32d163215027aa0;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/filter/tests/src/Functional/FilterAdminTest.php b/web/core/modules/filter/tests/src/Functional/FilterAdminTest.php new file mode 100644 index 000000000..21431e796 --- /dev/null +++ b/web/core/modules/filter/tests/src/Functional/FilterAdminTest.php @@ -0,0 +1,458 @@ +drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); + + // Set up the filter formats used by this test. + $basic_html_format = FilterFormat::create([ + 'format' => 'basic_html', + 'name' => 'Basic HTML', + 'filters' => [ + 'filter_html' => [ + 'status' => 1, + 'settings' => [ + 'allowed_html' => '


', + ], + ], + ], + ]); + $basic_html_format->save(); + $restricted_html_format = FilterFormat::create([ + 'format' => 'restricted_html', + 'name' => 'Restricted HTML', + 'filters' => [ + 'filter_html' => [ + 'status' => TRUE, + 'weight' => -10, + 'settings' => [ + 'allowed_html' => '


', + ], + ], + 'filter_autop' => [ + 'status' => TRUE, + 'weight' => 0, + ], + 'filter_url' => [ + 'status' => TRUE, + 'weight' => 0, + ], + 'filter_htmlcorrector' => [ + 'status' => TRUE, + 'weight' => 10, + ], + ], + ]); + $restricted_html_format->save(); + $full_html_format = FilterFormat::create([ + 'format' => 'full_html', + 'name' => 'Full HTML', + 'weight' => 1, + 'filters' => [], + ]); + $full_html_format->save(); + + $this->adminUser = $this->drupalCreateUser([ + 'administer filters', + $basic_html_format->getPermissionName(), + $restricted_html_format->getPermissionName(), + $full_html_format->getPermissionName(), + 'access site reports', + ]); + + $this->webUser = $this->drupalCreateUser(['create page content', 'edit own page content']); + user_role_grant_permissions('authenticated', [$basic_html_format->getPermissionName()]); + user_role_grant_permissions('anonymous', [$restricted_html_format->getPermissionName()]); + $this->drupalLogin($this->adminUser); + $this->drupalPlaceBlock('local_actions_block'); + } + + /** + * Tests the format administration functionality. + */ + public function testFormatAdmin() { + // Add text format. + $this->drupalGet('admin/config/content/formats'); + $this->clickLink('Add text format'); + $format_id = Unicode::strtolower($this->randomMachineName()); + $name = $this->randomMachineName(); + $edit = [ + 'format' => $format_id, + 'name' => $name, + ]; + $this->drupalPostForm(NULL, $edit, t('Save configuration')); + + // Verify default weight of the text format. + $this->drupalGet('admin/config/content/formats'); + $this->assertFieldByName("formats[$format_id][weight]", 0, 'Text format weight was saved.'); + + // Change the weight of the text format. + $edit = [ + "formats[$format_id][weight]" => 5, + ]; + $this->drupalPostForm('admin/config/content/formats', $edit, t('Save')); + $this->assertFieldByName("formats[$format_id][weight]", 5, 'Text format weight was saved.'); + + // Edit text format. + $this->drupalGet('admin/config/content/formats'); + // Cannot use the assertNoLinkByHref method as it does partial url matching + // and 'admin/config/content/formats/manage/' . $format_id . '/disable' + // exists. + // @todo: See https://www.drupal.org/node/2031223 for the above. + $edit_link = $this->xpath('//a[@href=:href]', [ + ':href' => \Drupal::url('entity.filter_format.edit_form', ['filter_format' => $format_id]) + ]); + $this->assertNotEmpty($edit_link, format_string('Link href %href found.', + ['%href' => 'admin/config/content/formats/manage/' . $format_id] + )); + $this->drupalGet('admin/config/content/formats/manage/' . $format_id); + $this->drupalPostForm(NULL, [], t('Save configuration')); + + // Verify that the custom weight of the text format has been retained. + $this->drupalGet('admin/config/content/formats'); + $this->assertFieldByName("formats[$format_id][weight]", 5, 'Text format weight was retained.'); + + // Disable text format. + $this->assertLinkByHref('admin/config/content/formats/manage/' . $format_id . '/disable'); + $this->drupalGet('admin/config/content/formats/manage/' . $format_id . '/disable'); + $this->drupalPostForm(NULL, [], t('Disable')); + + // Verify that disabled text format no longer exists. + $this->drupalGet('admin/config/content/formats/manage/' . $format_id); + $this->assertResponse(404, 'Disabled text format no longer exists.'); + + // Attempt to create a format of the same machine name as the disabled + // format but with a different human readable name. + $edit = [ + 'format' => $format_id, + 'name' => 'New format', + ]; + $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration')); + $this->assertText('The machine-readable name is already in use. It must be unique.'); + + // Attempt to create a format of the same human readable name as the + // disabled format but with a different machine name. + $edit = [ + 'format' => 'new_format', + 'name' => $name, + ]; + $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration')); + $this->assertRaw(t('Text format names must be unique. A format named %name already exists.', [ + '%name' => $name, + ])); + } + + /** + * Tests filter administration functionality. + */ + public function testFilterAdmin() { + $first_filter = 'filter_autop'; + $second_filter = 'filter_url'; + + $basic = 'basic_html'; + $restricted = 'restricted_html'; + $full = 'full_html'; + $plain = 'plain_text'; + + // Check that the fallback format exists and cannot be disabled. + $this->assertTrue($plain == filter_fallback_format(), 'The fallback format is set to plain text.'); + $this->drupalGet('admin/config/content/formats'); + $this->assertNoRaw('admin/config/content/formats/manage/' . $plain . '/disable', 'Disable link for the fallback format not found.'); + $this->drupalGet('admin/config/content/formats/manage/' . $plain . '/disable'); + $this->assertResponse(403, 'The fallback format cannot be disabled.'); + + // Verify access permissions to Full HTML format. + $full_format = FilterFormat::load($full); + $this->assertTrue($full_format->access('use', $this->adminUser), 'Admin user may use Full HTML.'); + $this->assertFalse($full_format->access('use', $this->webUser), 'Web user may not use Full HTML.'); + + // Add an additional tag and extra spaces and returns. + $edit = []; + $edit['filters[filter_html][settings][allowed_html]'] = "