Commit c02791c7 authored by anon's avatar anon

by anon: Converted simpletests to Kernal tests and Browser tests.

parent cbdfc05f
<?php
namespace Drupal\linkit\Tests;
use Drupal\file\Entity\File;
/**
* Tests the Linkit filter for file entities.
*
* @group linkit
*/
class LinkitFilterFileEntityTest extends LinkitFilterTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['file'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
}
/**
* Tests the linkit filter for file entities.
*/
public function testFilterFileEntity() {
$file = File::create(array(
'uid' => 1,
'filename' => 'druplicon.txt',
'uri' => 'public://druplicon.txt',
'filemime' => 'text/plain',
'status' => FILE_STATUS_PERMANENT,
));
$file->save();
// Disable the automatic title attribute.
$this->filter->setConfiguration(['settings' => ['title' => 0]]);
$this->assertLinkitFilter($file);
// Automatically set the title.
$this->filter->setConfiguration(['settings' => ['title' => 1]]);
$this->assertLinkitFilterWithTitle($file);
}
}
<?php
namespace Drupal\linkit\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Url;
/**
* Tests creating, loading and deleting profiles.
*
* @group linkit
*/
class ProfileCrudTest extends LinkitTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
}
/**
* Test the overview page.
*/
public function testOverview() {
// Verify that the profile collection page is not accessible for regular
// users.
$this->drupalLogin($this->baseUser);
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
$this->assertResponse(403);
$this->drupalLogout();
// Verify that the profile collection page is accessible for regular users.
$this->drupalLogin($this->adminUser);
$profiles = [];
$profiles[] = $this->createProfile();
$profiles[] = $this->createProfile();
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
$this->assertResponse(200);
// Assert that the 'Add profile' action exists.
$this->assertLinkByHref(Url::fromRoute('entity.linkit_profile.add_form')->toString());
/** @var \Drupal\linkit\ProfileInterface $profile */
foreach ($profiles as $profile) {
$this->assertLinkByHref('admin/config/content/linkit/manage/' . $profile->id());
$this->assertLinkByHref('admin/config/content/linkit/manage/' . $profile->id() . '/delete');
}
}
/**
* Creates profile.
*/
public function testProfileCreation() {
$this->drupalGet(Url::fromRoute('entity.linkit_profile.add_form'));
$this->drupalGet('admin/config/content/linkit/add');
$this->assertResponse(200);
// Create a profile.
$edit = [];
$edit['label'] = Unicode::strtolower($this->randomMachineName());
$edit['id'] = Unicode::strtolower($this->randomMachineName());
$edit['description'] = $this->randomMachineName(16);
$this->drupalPostForm(NULL, $edit, t('Save and manage matchers'));
$this->assertRaw(t('Created new profile %label.', ['%label' => $edit['label']]));
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
$this->assertText($edit['label'], 'Profile exists in the profile collection.');
}
/**
* Updates a profile.
*/
public function testProfileUpdate() {
$profile = $this->createProfile();
$this->drupalGet(Url::fromRoute('entity.linkit_profile.edit_form', [
'linkit_profile' => $profile->id(),
]));
$this->assertResponse(200);
$id_field = $this->xpath('.//input[not(@disabled) and @name="id"]');
$this->assertTrue(empty($id_field), 'Machine name field is disabled.');
$this->assertLinkByHref(Url::fromRoute('entity.linkit_profile.edit_form', [
'linkit_profile' => $profile->id(),
])->toString());
$this->assertLinkByHref('admin/config/content/linkit/manage/' . $profile->id() . '/delete');
$edit = [];
$edit['label'] = $this->randomMachineName();
$edit['description'] = $this->randomMachineName(16);
$this->drupalPostForm(NULL, $edit, t('Update profile'));
$this->assertRaw(t('Updated profile %label.', ['%label' => $edit['label']]));
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
$this->assertText($edit['label'], 'Updated profile exists in the profile collection.');
}
/**
* Delete a profile.
*/
public function testProfileDelete() {
/** @var \Drupal\linkit\ProfileInterface $profile */
$profile = $this->createProfile();
$this->drupalGet(Url::fromRoute('entity.linkit_profile.delete_form', [
'linkit_profile' => $profile->id(),
]));
$this->drupalPostForm(NULL, [], t('Delete'));
$this->assertRaw(t('The linkit profile %label has been deleted.', ['%label' => $profile->label()]));
$this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
$this->assertNoText($profile->label(), 'Deleted profile does not exists in the profile collection.');
}
}
<?php
namespace Drupal\linkit\Tests\Controllers;
namespace Drupal\Tests\linkit\Functional\Controller;
use Drupal\Core\Url;
use Drupal\linkit\Tests\LinkitTestBase;
use Drupal\Tests\linkit\Functional\LinkitBrowserTestBase;
use Drupal\Tests\linkit\ProfileCreationTrait;
/**
* Tests Linkit controller.
*
* @group linkit
*/
class LinkitControllerTest extends LinkitTestBase {
class LinkitControllerTest extends LinkitBrowserTestBase {
use ProfileCreationTrait;
/**
* The linkit profile.
......@@ -26,33 +29,33 @@ class LinkitControllerTest extends LinkitTestBase {
parent::setUp();
$this->linkitProfile = $this->createProfile();
$this->drupalLogin($this->adminUser);
}
/**
* Tests the profile route title callback.
*/
public function testProfileTitle() {
$this->drupalGet(Url::fromRoute('entity.linkit_profile.edit_form', [
'linkit_profile' => $this->linkitProfile->id(),
]));
$this->drupalLogin($this->adminUser);
$this->assertText('Edit ' . $this->linkitProfile->label() . ' profile');
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id());
$this->assertSession()->pageTextContains('Edit ' . $this->linkitProfile->label() . ' profile');
}
/**
* Tests the matcher route title callback.
*/
public function testMatcherTitle() {
$this->drupalLogin($this->adminUser);
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->container->get('plugin.manager.linkit.matcher')->createInstance('configurable_dummy_matcher');
$matcher_uuid = $this->linkitProfile->addMatcher($plugin->getConfiguration());
$this->linkitProfile->save();
$this->drupalGet(Url::fromRoute('linkit.matcher.edit', ['linkit_profile' => $this->linkitProfile->id(), 'plugin_instance_id' => $matcher_uuid]));
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers/' . $matcher_uuid);
$this->assertText('Edit ' . $plugin->getLabel() . ' matcher');
$this->assertSession()->pageTextContains('Edit ' . $plugin->getLabel() . ' matcher');
}
}
<?php
namespace Drupal\linkit\Tests;
namespace Drupal\Tests\linkit\Functional;
use Drupal\Core\Url;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\linkit\ProfileCreationTrait;
/**
* Tests the IMCE module integration.
*
* @group linkit
*/
class ImceIntegrationTest extends LinkitTestBase {
class ImceIntegrationTest extends LinkitBrowserTestBase {
use ProfileCreationTrait;
/**
* Modules to enable.
......@@ -73,7 +75,7 @@ class ImceIntegrationTest extends LinkitTestBase {
$this->editor->save();
// Create a regular user with access to the format.
$this->baseUser = $this->drupalCreateUser([
$this->webUser = $this->drupalCreateUser([
$this->filterFormat->getPermissionName(),
]);
}
......@@ -82,43 +84,33 @@ class ImceIntegrationTest extends LinkitTestBase {
* Test that the IMCE link does not shows up.
*/
public function testImceIntegationDisabled() {
$this->drupalLogin($this->baseUser);
$this->drupalGet(Url::fromRoute('editor.link_dialog', [
'filter_format' => $this->filterFormat->id(),
]));
$this->assertNoLink('Open IMCE file browser');
$this->drupalLogin($this->webUser);
$this->drupalGet('editor/dialog/link/' . $this->filterFormat->id());
$this->assertSession()->linkNotExists('Open IMCE file browser');
}
/**
* Test that the IMCE link shows up.
*/
public function testImceIntegationEnabled() {
$this->drupalGet(Url::fromRoute('entity.linkit_profile.edit_form', [
'linkit_profile' => $this->linkitProfile->id(),
]));
$this->assertResponse(200);
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id());
$this->assertSession()->statusCodeEquals(200);
$this->assertText('IMCE integration');
$this->assertFieldByName('imce_use');
$this->assertSession()->pageTextContains('IMCE integration');
$this->assertSession()->fieldExists('imce_use');
$edit = [];
$edit['imce_use'] = TRUE;
$this->drupalPostForm(NULL, $edit, t('Update profile'));
$this->drupalGet(Url::fromRoute('entity.linkit_profile.edit_form', [
'linkit_profile' => $this->linkitProfile->id(),
]));
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id());
$this->assertFieldChecked('edit-imce-use');
$this->assertSession()->fieldValueEquals('edit-imce-use', '1');
$this->drupalLogin($this->baseUser);
$this->drupalLogin($this->webUser);
$this->drupalGet(Url::fromRoute('editor.link_dialog', [
'filter_format' => $this->filterFormat->id(),
]));
$this->assertLink('Open IMCE file browser');
$this->drupalGet('editor/dialog/link/' . $this->filterFormat->id());
$this->assertSession()->linkExists('Open IMCE file browser');
}
}
<?php
namespace Drupal\Tests\linkit\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Provides a base class for Linkit functional tests.
*/
abstract class LinkitBrowserTestBase extends BrowserTestBase {
/**
* Modules to install.
*
* @var array
*/
public static $modules = ['linkit', 'linkit_test', 'block'];
/**
* A test user with administrative privileges.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* A user without the 'administer linkit profiles' permission.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->placeBlock('page_title_block');
$this->placeBlock('local_tasks_block');
$this->placeBlock('local_actions_block');
$this->placeBlock('system_messages_block');
$this->adminUser = $this->drupalCreateUser(['administer linkit profiles']);
$this->webUser = $this->drupalCreateUser();
}
}
\ No newline at end of file
<?php
namespace Drupal\linkit\Tests;
namespace Drupal\Tests\linkit\Functional;
use Drupal\Core\Url;
use Drupal\linkit\Entity\Profile;
use Drupal\Tests\linkit\ProfileCreationTrait;
/**
* Tests adding, listing, updating and deleting matchers on a profile.
*
* @group linkit
*/
class MatcherCrudTest extends LinkitTestBase {
class MatcherAdminTest extends LinkitBrowserTestBase {
use ProfileCreationTrait;
/**
* The attribute manager.
......@@ -34,121 +36,103 @@ class MatcherCrudTest extends LinkitTestBase {
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
$this->linkitProfile = $this->createProfile();
$this->drupalLogin($this->adminUser);
}
/**
* Test the overview page.
*/
public function testOverview() {
$this->drupalGet(Url::fromRoute('linkit.matchers', [
'linkit_profile' => $this->linkitProfile->id(),
]));
$this->assertText(t('No matchers added.'));
$this->assertLinkByHref(Url::fromRoute('linkit.matcher.add', [
'linkit_profile' => $this->linkitProfile->id(),
])->toString());
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers');
$this->assertSession()->pageTextContains(t('No matchers added.'));
// Make sure the 'Add matcher' action link is present.
$this->assertSession()->linkByHrefExists('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers/add');
}
/**
* Test adding a matcher to a profile.
*/
public function testAdd() {
$this->drupalGet(Url::fromRoute('linkit.matcher.add', [
'linkit_profile' => $this->linkitProfile->id(),
]));
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers/add');
// Create matcher.
$edit = [];
$edit['plugin'] = 'dummy_matcher';
$this->drupalPostForm(NULL, $edit, t('Save and continue'));
$this->submitForm($edit, t('Save and continue'));
// Load the saved profile.
// Reload the profile.
$this->linkitProfile = Profile::load($this->linkitProfile->id());
$matcher_ids = $this->linkitProfile->getMatchers()->getInstanceIds();
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->linkitProfile->getMatcher(current($matcher_ids));
$this->assertRaw(t('Added %label matcher.', ['%label' => $plugin->getLabel()]));
$this->assertNoText(t('No matchers added.'));
$this->assertSession()->responseContains(t('Added %label matcher.', ['%label' => $plugin->getLabel()]));
$this->assertSession()->pageTextNotContains(t('No matchers added.'));
}
/**
* Test adding a configurable attribute to a profile.
*/
public function testAddConfigurable() {
$this->drupalGet(Url::fromRoute('linkit.matcher.add', [
'linkit_profile' => $this->linkitProfile->id(),
]));
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers/add');
// Create configurable matcher.
$edit = [];
$edit['plugin'] = 'configurable_dummy_matcher';
$this->drupalPostForm(NULL, $edit, t('Save and continue'));
$this->submitForm($edit, t('Save and continue'));
// Load the saved profile.
// Reload the profile.
$this->linkitProfile = Profile::load($this->linkitProfile->id());
$matcher_ids = $this->linkitProfile->getMatchers()->getInstanceIds();
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->linkitProfile->getMatcher(current($matcher_ids));
$this->assertUrl(Url::fromRoute('linkit.matcher.edit', [
'linkit_profile' => $this->linkitProfile->id(),
'plugin_instance_id' => $plugin->getUuid(),
]));
$this->drupalGet(Url::fromRoute('linkit.matchers', [
'linkit_profile' => $this->linkitProfile->id(),
]));
$this->assertSession()->addressEquals('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers/' . $plugin->getUuid());
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers');
$this->assertNoText(t('No matchers added.'));
$this->assertSession()->pageTextNotContains(t('No matchers added.'));
}
/**
* Test delete a matcher from a profile.
*/
public function testDelete() {
$this->drupalLogin($this->adminUser);
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('dummy_matcher');
$profile = $this->createProfile();
$plugin_uuid = $profile->addMatcher($plugin->getConfiguration());
$profile->save();
$plugin_uuid = $this->linkitProfile->addMatcher($plugin->getConfiguration());
$this->linkitProfile->save();
// Try delete a matcher that is not attached to the profile.
$this->drupalGet(Url::fromRoute('linkit.matcher.delete', [
'linkit_profile' => $profile->id(),
'plugin_instance_id' => 'doesntexists',
]));
$this->assertResponse('404');
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers/doesntexists/delete');
$this->assertSession()->statusCodeEquals('404');
// Go to the delete page, but press cancel.
$this->drupalGet(Url::fromRoute('linkit.matcher.delete', [
'linkit_profile' => $profile->id(),
'plugin_instance_id' => $plugin_uuid,
]));
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers/' . $plugin_uuid . '/delete');
$this->clickLink(t('Cancel'));
$this->assertUrl(Url::fromRoute('linkit.matchers', [
'linkit_profile' => $profile->id(),
]));
$this->assertSession()->addressEquals('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers');
// Delete the matcher from the profile.
$this->drupalGet(Url::fromRoute('linkit.matcher.delete', [
'linkit_profile' => $profile->id(),
'plugin_instance_id' => $plugin_uuid,
]));
$this->drupalPostForm(NULL, [], t('Confirm'));
$this->assertRaw(t('The matcher %plugin has been deleted.', ['%plugin' => $plugin->getLabel()]));
$this->assertUrl(Url::fromRoute('linkit.matchers', [
'linkit_profile' => $profile->id(),
]));
$this->assertText(t('No matchers added.'));
$this->drupalGet('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers/' . $plugin_uuid . '/delete');
$this->submitForm([], t('Confirm'));
$this->assertSession()->responseContains(t('The matcher %plugin has been deleted.', ['%plugin' => $plugin->getLabel()]));
$this->assertSession()->addressEquals('/admin/config/content/linkit/manage/' . $this->linkitProfile->id() . '/matchers');
$this->assertSession()->pageTextContains(t('No matchers added.'));
/** @var \Drupal\linkit\Entity\Profile $updated_profile */
$updated_profile = Profile::load($profile->id());
$updated_profile = Profile::load($this->linkitProfile->id());
$this->assertFalse($updated_profile->getMatchers()->has($plugin_uuid), 'The user matcher is deleted from the profile');
}
}
}
\ No newline at end of file
<?php
namespace Drupal\Tests\linkit\Functional;
use Drupal\Component\Utility\Unicode;
use Drupal\Tests\linkit\ProfileCreationTrait;
/**
* Tests creating, loading and deleting profiles.
*
* @group linkit
*/
class ProfileAdminTest extends LinkitBrowserTestBase {
use ProfileCreationTrait;
/**
* Test the overview page.
*/
public function testOverview() {
// Verify that the profile collection page is not accessible for users
// without permission.
$this->drupalLogin($this->webUser);
$this->drupalGet('/admin/config/content/linkit');
$this->assertSession()->statusCodeEquals(403);
$this->drupalLogout();
// Login as an admin user and make sure the collection page is accessible.
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/content/linkit');
$this->assertSession()->statusCodeEquals(200);
// Make sure the 'Add profile' action link is present.
$this->assertSession()->linkByHrefExists('/admin/config/content/linkit/add');
// Create multiple profiles.
$profiles = [];
$profiles[] = $this->createProfile();
$profiles[] = $this->createProfile();
// Refresh the page.
$this->drupalGet('/admin/config/content/linkit');
$this->assertSession()->statusCodeEquals(200);
// Make sure that there is an edit and a delete operation link for all
// profiles.
foreach ($profiles as $profile) {
$this->assertSession()->linkByHrefExists('/admin/config/content/linkit/manage/' . $profile->id());
$this->assertSession()->linkByHrefExists('/admin/config/content/linkit/manage/' . $profile->id() . '/delete');
}
}
/**
* Creates profile.
*/
public function testProfileCreation() {
$this->drupalLogin($this->adminUser);
// Make sure the profile add page is accessible.
$this->drupalGet('/admin/config/content/linkit/add');
$this->assertSession()->statusCodeEquals(200);
// Create a profile.
$edit = [];
$edit['label'] = Unicode::strtolower($this->randomMachineName());
$edit['id'] = Unicode::strtolower($this->randomMachineName());
$edit['description'] = $this->randomMachineName(16);
$this->submitForm($edit, t('Save and manage matchers'));
// Make sure that the new profile was saved properly.
$this->assertSession()->responseContains(t('Created new profile %label.', ['%label' => $edit['label']]));
$this->drupalGet('/admin/config/content/linkit');
$this->assertSession()->pageTextContains($edit['label']);
}
/**
* Updates a profile.
*/
public function testProfileUpdate() {
$this->drupalLogin($this->adminUser);
// Create a profile.
$profile = $this->createProfile();
// Make sure the profile edit page is accessible.
$this->drupalGet('/admin/config/content/linkit/manage/' . $profile->id());
$this->assertSession()->statusCodeEquals(200);
// Make sure the machine name field is disabled and that we have certain
// elements presented.
$this->assertSession()->elementNotExists('xpath', '//input[not(@disabled) and @name="id"]');
$this->assertSession()->buttonExists('Update profile');
$this->assertSession()->linkByHrefExists('/admin/config/content/linkit/manage/' . $profile->id() . '/delete');
// Update the profile.
$edit = [];
$edit['label'] = $this->randomMachineName();
$edit['description'] = $this->randomMachineName(16);
$this->submitForm($edit, t('Update profile'));
// Make sure that the profile was updated properly.
$this->assertSession()->responseContains(t('Updated profile %label.', ['%label' => $edit['label']]));
$this->drupalGet('/admin/config/content/linkit');
$this->assertSession()->pageTextContains($edit['label']);
}
/**
* Delete a profile.
*/
public function testProfileDelete() {
$this->drupalLogin($this->adminUser);
// Create a profile.
$profile = $this->createProfile();
$this->drupalGet('/admin/config/content/linkit/manage/' . $profile->id() . '/delete');
$this->assertSession()->statusCodeEquals(200);
// Delete the profile.
$this->submitForm([], t('Delete'));
// Make sure that the profile was deleted properly.
$this->assertSession()->responseContains(t('The linkit profile %label has been deleted.', ['%label' => $profile->label()]));
$this->drupalGet('/admin/config/content/linkit');
$this->assertSession()->responseNotContains($profile->label());
}
}
\ No newline at end of file
<?php
namespace Drupal\linkit\Tests;
namespace Drupal\Tests\linkit\Kernel;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\filter\FilterPluginCollection;
/**
* Provides a base class for testing the Linkit filter.
* Provides helper methods for assertions.
*/
abstract class LinkitFilterTestBase extends LinkitTestBase {
trait AssertLinkitFilterTrait {
/**
* The linkit filter.
......@@ -19,23 +18,13 @@ abstract class LinkitFilterTestBase extends LinkitTestBase {
*/
protected $filter;
/**
* {@inheritdoc}
*/
protected function setUp() {