View source
<?php
namespace Drupal\Tests\field_group\FunctionalJavascript;
use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\Core\Extension\ThemeInstallerInterface;
use Drupal\Core\Url;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\field_group\Functional\FieldGroupTestTrait;
class HorizontalTabsLabelsTest extends WebDriverTestBase {
use FieldGroupTestTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'block',
'field_group',
'node',
'user',
];
protected $themeList = [
'bartik',
'claro',
'classy',
'seven',
'stable',
'stable9',
'stark',
];
protected $themesWithBlocks = [
'claro',
];
protected $assertSession;
protected $page;
protected $testNodeType;
public function setUp() {
parent::setUp();
$this->assertSession = $this
->assertSession();
$this->page = $this
->getSession()
->getPage();
$this->testNodeType = $this
->drupalCreateContentType([
'type' => 'test_node_bundle',
'name' => 'Test Node Type',
]);
$entity_type_manager = $this->container
->get('entity_type.manager');
$field_storage = $entity_type_manager
->getStorage('field_storage_config')
->create([
'type' => 'string',
'field_name' => 'test_label',
'entity_type' => 'node',
]);
assert($field_storage instanceof FieldStorageConfigInterface);
$field_storage
->save();
$entity_type_manager
->getStorage('field_config')
->create([
'label' => 'Test label',
'field_storage' => $field_storage,
'bundle' => $this->testNodeType
->id(),
])
->save();
$tab1 = [
'label' => 'Tab1',
'group_name' => 'group_tab1',
'weight' => '1',
'children' => [
0 => 'test_label',
],
'format_type' => 'tab',
'format_settings' => [
'label' => 'Tab1',
'formatter' => 'open',
],
];
$this
->createGroup('node', $this->testNodeType
->id(), 'form', 'default', $tab1);
$this
->createGroup('node', $this->testNodeType
->id(), 'view', 'default', $tab1);
$tab2 = [
'label' => 'Tab2',
'group_name' => 'group_tab2',
'weight' => '2',
'children' => [
0 => 'body',
],
'format_type' => 'tab',
'format_settings' => [
'label' => 'Tab2',
'formatter' => 'closed',
],
];
$this
->createGroup('node', $this->testNodeType
->id(), 'form', 'default', $tab2);
$this
->createGroup('node', $this->testNodeType
->id(), 'view', 'default', $tab2);
$horizontal_tabs = [
'label' => 'Horizontal tabs',
'group_name' => 'group_horizontal_tabs',
'weight' => '-5',
'children' => [
'group_tab1',
'group_tab2',
],
'format_type' => 'tabs',
'format_settings' => [
'direction' => 'horizontal',
'label' => 'Horizontal tabs',
],
];
$this
->createGroup('node', $this->testNodeType
->id(), 'form', 'default', $horizontal_tabs);
$this
->createGroup('node', $this->testNodeType
->id(), 'view', 'default', $horizontal_tabs);
$entity_type_manager
->getStorage('entity_form_display')
->load(implode('.', [
'node',
$this->testNodeType
->id(),
'default',
]))
->setComponent('test_label', [
'weight' => '1',
])
->save();
$entity_type_manager
->getStorage('entity_view_display')
->load(implode('.', [
'node',
$this->testNodeType
->id(),
'default',
]))
->setComponent('test_label', [
'weight' => '1',
])
->save();
}
public function testHorizontalTabsLabels(string $theme_name) {
if ($theme_name !== $this->defaultTheme) {
$theme_installer = \Drupal::service('theme_installer');
assert($theme_installer instanceof ThemeInstallerInterface);
try {
$theme_installer
->install([
$theme_name,
], TRUE);
} catch (UnknownExtensionException $ex) {
$this
->pass("The {$theme_name} theme does not exist in the current test environment.");
return;
}
\Drupal::configFactory()
->getEditable('system.theme')
->set('default', $theme_name)
->set('admin', $theme_name)
->save();
}
if (!in_array($theme_name, $this->themesWithBlocks, TRUE)) {
$this
->drupalPlaceBlock('page_title_block', [
'region' => 'content',
]);
$this
->drupalPlaceBlock('local_tasks_block', [
'region' => 'content',
'weight' => 1,
]);
$this
->drupalPlaceBlock('local_actions_block', [
'region' => 'content',
'weight' => 2,
]);
$this
->drupalPlaceBlock('system_main_block', [
'region' => 'content',
'weight' => 3,
]);
}
$this
->drupalLogin($this->rootUser);
$this
->drupalGet(Url::fromRoute('node.add', [
'node_type' => $this->testNodeType
->id(),
]));
$this
->assertHorizontalTabsLabels();
$this->page
->fillField('title[0][value]', 'Field Group Horizontal Tabs Test Node');
$this->page
->fillField('Test label', 'Test label');
$this
->assertNotNull($tab2 = $this->page
->find('css', '.js .field-group-tabs-wrapper a[href="#edit-group-tab2"]'));
$tab2
->click();
$this->assertSession
->waitForElementVisible('css', '[name="body[0][value]"]');
$this->page
->fillField('body[0][value]', 'Donec laoreet imperdiet.');
$this->page
->findButton('edit-submit')
->click();
$this->assertSession
->waitForElement('css', 'html.js [data-drupal-messages]');
$status_message = $this->page
->find('css', 'html.js [data-drupal-messages]');
$this
->assertStringContainsString("{$this->testNodeType->label()} Field Group Horizontal Tabs Test Node has been created.", $status_message
->getText());
$this
->drupalGet(Url::fromRoute('entity.node.canonical', [
'node' => '1',
]));
$this
->assertHorizontalTabsLabels();
$this
->drupalLogout();
$this
->drupalGet(Url::fromRoute('entity.node.canonical', [
'node' => '1',
]));
$this
->assertHorizontalTabsLabels();
}
protected function assertHorizontalTabsLabels() {
$this->assertSession
->waitForElement('css', '.js .field-group-tabs-wrapper a[href="#edit-group-tab1"]');
$this->assertSession
->waitForElement('css', '.js .field-group-tabs-wrapper a[href="#edit-group-tab2"]');
$this
->assertNotNull($tab1 = $this->page
->find('css', '.js .field-group-tabs-wrapper a[href="#edit-group-tab1"]'));
$this
->assertStringContainsString('Tab1', $tab1
->getText());
$this
->assertNotNull($tab2 = $this->page
->find('css', '.js .field-group-tabs-wrapper a[href="#edit-group-tab2"]'));
$this
->assertStringContainsString('Tab2', $tab2
->getText());
}
public function providerTestHorizontalTabsLabels() {
return array_reduce($this->themeList, function (array $carry, string $theme_name) {
$carry[$theme_name] = [
'theme_name' => $theme_name,
];
return $carry;
}, []);
}
}