View source
<?php
namespace Drupal\Tests\coffee\Functional;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Util\Json;
class CoffeeTest extends BrowserTestBase {
public static $modules = [
'coffee',
'coffee_test',
'menu_link_content',
];
protected $webUser;
protected $coffeeUser;
protected $coffeeAdmin;
public function setUp() {
parent::setUp();
$this->webUser = $this
->drupalCreateUser();
$this->coffeeUser = $this
->drupalCreateUser([
'access coffee',
]);
$this->coffeeAdmin = $this
->drupalCreateUser([
'administer coffee',
]);
}
public function testCoffeeConfiguration() {
$this
->drupalGet('admin/config/user-interface/coffee');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->coffeeAdmin);
$this
->drupalGet('admin/config/user-interface/coffee');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->checkboxChecked('edit-coffee-menus-admin');
$this
->assertSession()
->fieldValueEquals('edit-max-results', 7);
$edit = [
'coffee_menus[tools]' => 'tools',
'coffee_menus[account]' => 'account',
'max_results' => 15,
];
$this
->drupalPostForm('admin/config/user-interface/coffee', $edit, t('Save configuration'));
$this
->assertSession()
->pageTextContains(t('The configuration options have been saved.'));
$expected = [
'admin' => 'admin',
'tools' => 'tools',
'account' => 'account',
];
$config = \Drupal::config('coffee.configuration')
->get('coffee_menus');
$this
->assertEquals($expected, $config, 'The configuration options have been properly saved');
$config = \Drupal::config('coffee.configuration')
->get('max_results');
$this
->assertEquals(15, $config, 'The configuration options have been properly saved');
}
public function testCoffeeCacheTagsInvalidation() {
$coffee_cache_tag = 'config:coffee.configuration';
$this
->drupalGet('');
$this
->assertSession()
->responseHeaderNotContains('X-Drupal-Cache-Tags', $coffee_cache_tag);
$this
->drupalLogin($this->coffeeUser);
$this
->drupalGet('');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', $coffee_cache_tag);
$settings = $this
->getDrupalSettings();
$this
->assertEquals(7, $settings['coffee']['maxResults']);
$max_results = 10;
$this
->config('coffee.configuration')
->set('max_results', $max_results)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', $coffee_cache_tag);
$settings = $this
->getDrupalSettings();
$this
->assertEquals($max_results, $settings['coffee']['maxResults']);
}
public function testCoffeeAssets() {
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('coffee/js/coffee.js');
$this
->drupalLogin($this->coffeeUser);
$this
->drupalGet('');
$this
->assertSession()
->responseContains('coffee/js/coffee.js');
$this
->drupalLogin($this->webUser);
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('coffee/js/coffee.js');
}
public function testCoffeeToolbarIntegration() {
\Drupal::service('module_installer')
->install([
'toolbar',
]);
$tab_xpath = '//nav[@id="toolbar-bar"]//div/a[contains(@class, "toolbar-icon-coffee")]';
$toolbar_user = $this
->drupalCreateUser([
'access toolbar',
]);
$this
->drupalLogin($toolbar_user);
$this
->assertSession()
->responseContains('id="toolbar-administration"');
$this
->assertSession()
->elementNotExists('xpath', $tab_xpath);
$coffee_toolbar_user = $this
->drupalCreateUser([
'access toolbar',
'access coffee',
]);
$this
->drupalLogin($coffee_toolbar_user);
$this
->assertSession()
->responseContains('id="toolbar-administration"');
$this
->assertSession()
->elementExists('xpath', $tab_xpath);
}
public function testCoffeeCsrf() {
$account = $this
->drupalCreateUser([
'access coffee',
'access administration pages',
]);
$this
->drupalLogin($account);
$menu = Menu::create([
'id' => 'coffee',
'label' => 'Coffee',
'description' => 'Menu for testing Coffee.',
]);
$menu
->save();
$menu_link = MenuLinkContent::create([
'title' => 'Coffee test',
'provider' => 'menu_link_content',
'menu_name' => 'coffee',
'link' => [
'uri' => 'internal:/coffee-test-csrf',
],
]);
$menu_link
->save();
$this
->config('coffee.configuration')
->set('coffee_menus', [
'coffee',
])
->save();
$result = $this
->drupalGet('/admin/coffee/get-data');
$result = json_decode($result);
$token = substr($result[0]->value, strpos($result[0]->value, 'token=') + 6);
$this
->drupalGet('/coffee-test-csrf', [
'query' => [
'token' => $token,
],
]);
$this
->assertResponse(200);
}
}