View source
<?php
namespace Drupal\Tests\demo_umami\Functional;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\KernelTests\AssertConfigTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Render\FormattableMarkup;
class DemoUmamiProfileTest extends BrowserTestBase {
use AssertConfigTrait;
protected function installParameters() {
$parameters = parent::installParameters();
$parameters['forms']['install_configure_form']['site_mail'] = 'admin@example.com';
return $parameters;
}
protected $profile = 'demo_umami';
public function testWarningsOnStatusPage() {
$account = $this
->drupalCreateUser([
'administer site configuration',
]);
$this
->drupalLogin($account);
$this
->drupalGet('admin/reports/status');
$this
->assertSession()
->pageTextContains('Experimental profiles are provided for testing purposes only. Use at your own risk. To start building a new site, reinstall Drupal and choose a non-experimental profile.');
}
public function testConfig() {
$active_config_storage = $this->container
->get('config.storage');
$default_config_storage = new FileStorage($this->container
->get('extension.list.profile')
->getPath('demo_umami') . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY, InstallStorage::DEFAULT_COLLECTION);
$this
->assertDefaultConfig($default_config_storage, $active_config_storage);
$default_config_storage = new FileStorage($this->container
->get('extension.list.profile')
->getPath('demo_umami') . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY, InstallStorage::DEFAULT_COLLECTION);
$this
->assertDefaultConfig($default_config_storage, $active_config_storage);
}
protected function assertDefaultConfig(StorageInterface $default_config_storage, StorageInterface $active_config_storage) {
$config_manager = $this->container
->get('config.manager');
foreach ($default_config_storage
->listAll() as $config_name) {
if ($active_config_storage
->exists($config_name)) {
$result = $config_manager
->diff($default_config_storage, $active_config_storage, $config_name);
$this
->assertConfigDiff($result, $config_name, [
'filter.format.basic_html' => [
'roles:',
' - authenticated',
],
'filter.format.full_html' => [
'roles:',
' - administrator',
],
'filter.format.restricted_html' => [
'roles:',
' - anonymous',
],
'system.site' => [
'uuid:',
'name:',
'mail:',
],
]);
}
else {
$this
->fail("{$config_name} has not been installed");
}
}
}
public function testUser() {
$password = $this->rootUser->pass_raw;
$ids = \Drupal::entityQuery('user')
->accessCheck(FALSE)
->condition('roles', [
'author',
'editor',
], 'IN')
->execute();
$users = \Drupal::entityTypeManager()
->getStorage('user')
->loadMultiple($ids);
foreach ($users as $user) {
$this
->drupalLoginWithPassword($user, $password);
}
}
public function testEditNodesByAdmin() {
$permissions = [
'administer nodes',
'edit any recipe content',
'use editorial transition create_new_draft',
];
$account = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($account);
$webassert = $this
->assertSession();
$nodes = $this->container
->get('entity_type.manager')
->getStorage('node')
->loadByProperties([
'title' => 'Deep mediterranean quiche',
]);
$node = reset($nodes);
$this
->drupalGet($node
->toUrl('edit-form'));
$webassert
->statusCodeEquals('200');
$this
->submitForm([], "Save");
$webassert
->pageTextContains('Recipe Deep mediterranean quiche has been updated.');
}
public function testAppearance() {
$account = $this
->drupalCreateUser([
'administer themes',
]);
$this
->drupalLogin($account);
$webassert = $this
->assertSession();
$this
->drupalGet('admin/appearance');
$webassert
->pageTextContains('Umami');
}
public function testDemonstrationWarningMessage() {
$permissions = [
'access content overview',
'access toolbar',
'administer nodes',
'edit any recipe content',
'create recipe content',
'use editorial transition create_new_draft',
];
$account = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($account);
$web_assert = $this
->assertSession();
$nodes = $this->container
->get('entity_type.manager')
->getStorage('node')
->loadByProperties([
'title' => 'Deep mediterranean quiche',
]);
$recipe_node = reset($nodes);
$this
->drupalGet($recipe_node
->toUrl('edit-form'));
$web_assert
->statusCodeEquals('200');
$web_assert
->pageTextContains('This site is intended for demonstration purposes.');
$this
->drupalGet('node/add/recipe');
$web_assert
->statusCodeEquals('200');
$web_assert
->pageTextContains('This site is intended for demonstration purposes.');
$this
->drupalGet('admin/content');
$web_assert
->statusCodeEquals('200');
$web_assert
->pageTextContains('This site is intended for demonstration purposes.');
$this
->drupalGet($recipe_node
->toUrl());
$web_assert
->statusCodeEquals('200');
$web_assert
->pageTextNotContains('This site is intended for demonstration purposes.');
$this
->drupalGet('<front>');
$web_assert
->statusCodeEquals('200');
$web_assert
->pageTextNotContains('This site is intended for demonstration purposes.');
}
protected function drupalLoginWithPassword(AccountInterface $account, $password) {
if ($this->loggedInUser) {
$this
->drupalLogout();
}
$this
->drupalGet('user/login');
$this
->submitForm([
'name' => $account
->getAccountName(),
'pass' => $password,
], 'Log in');
$account->sessionId = $this
->getSession()
->getCookie(\Drupal::service('session_configuration')
->getOptions(\Drupal::request())['name']);
$this
->assertTrue($this
->drupalUserIsLoggedIn($account), new FormattableMarkup('User %name successfully logged in.', [
'%name' => $account
->getAccountName(),
]));
$this->loggedInUser = $account;
$this->container
->get('current_user')
->setAccount($account);
}
}