View source
<?php
namespace Drupal\Tests\panelizer\Functional;
use Drupal\user\Entity\User;
trait PanelizerTestTrait {
protected function loginUser1() {
$account = User::load(1);
$password = 'foo';
$account
->setPassword($password)
->save();
$account->passRaw = $password;
$account->pass_raw = $password;
$this
->drupalLogin($account);
}
protected function setupContentType($content_type = 'page') {
$this
->loginUser1();
$this
->drupalCreateContentType([
'type' => $content_type,
'name' => 'Page',
]);
$this
->panelize($content_type, NULL, [
'panelizer[custom]' => TRUE,
]);
$this
->drupalLogout();
}
protected function createTestNode($type = 'page') {
return $this
->drupalCreateNode([
'title' => t('Hello, world!'),
'type' => $type,
]);
}
protected function panelize($content_type = 'page', $display = NULL, array $values = []) {
$this
->drupalGet("admin/structure/types");
$this
->assertResponse(200);
$this
->drupalGet("admin/structure/types/manage/{$content_type}");
$this
->assertResponse(200);
$path = "admin/structure/types/manage/{$content_type}/display";
if (!empty($display)) {
$path .= '/' . $display;
}
$this
->drupalGet($path);
$this
->assertResponse(200);
$edit = [
'panelizer[enable]' => TRUE,
] + $values;
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertResponse(200);
\Drupal::service('entity_display.repository')
->getFormDisplay('node', $content_type, 'default')
->setComponent('panelizer', [
'type' => 'panelizer',
])
->save();
}
protected function unpanelize($content_type = 'page', $display = NULL, array $values = []) {
$this
->drupalGet("admin/structure/types/manage/{$content_type}/display/{$display}");
$this
->assertResponse(200);
$edit = [
'panelizer[enable]' => FALSE,
] + $values;
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertResponse(200);
\Drupal::service('entity_display.repository')
->getFormDisplay('node', $content_type, 'default')
->removeComponent('panelizer')
->save();
}
protected function addPanelizerDefault($content_type = 'page', $display = 'default') {
$label = $this
->getRandomGenerator()
->word(16);
$id = strtolower($label);
$default_id = "node__{$content_type}__{$display}__{$id}";
$options = [
'query' => [
'js' => 'nojs',
],
];
$path = "admin/structure/types/manage/{$content_type}/display";
if (!empty($display)) {
$path .= '/' . $display;
}
$this
->drupalGet($path);
$this
->assertResponse(200);
$this
->clickLink('Add a new Panelizer default display');
$edit = [
'id' => $id,
'label' => $label,
];
$this
->drupalPostForm(NULL, $edit, t('Next'));
$this
->assertResponse(200);
$this
->assertUrl("admin/structure/panelizer/add/{$default_id}/contexts", $options);
$this
->drupalPostForm(NULL, [], t('Next'));
$this
->assertResponse(200);
$this
->assertUrl("admin/structure/panelizer/add/{$default_id}/layout", $options);
$this
->drupalPostForm(NULL, [], t('Next'));
$this
->assertResponse(200);
$layout_settings_form = $this
->getSession()
->getPage()
->find('css', '#panels-layout-settings-form');
if ($layout_settings_form) {
$layout_settings_form
->pressButton('Next');
}
$this
->assertUrl("admin/structure/panelizer/add/{$default_id}/content", $options);
$this
->drupalPostForm(NULL, [], t('Finish'));
$this
->assertResponse(200);
return $id;
}
protected function deletePanelizerDefault($content_type = 'page', $display = 'default', $id = 'default') {
$this
->drupalGet("admin/structure/panelizer/delete/node__{$content_type}__{$display}__{$id}");
$this
->assertResponse(200);
$this
->drupalPostForm(NULL, [], t('Confirm'));
$this
->assertResponse(200);
}
protected function assertDefaultExists($content_type = 'page', $display = 'default', $id = 'default') {
$settings = \Drupal::service('entity_display.repository')
->getViewDisplay('node', $content_type, $display)
->getThirdPartySettings('panelizer');
$display_exists = isset($settings['displays'][$id]);
$this
->assertTrue($display_exists);
}
protected function assertDefaultNotExists($content_type = 'page', $display = 'default', $id = 'default') {
$settings = \Drupal::service('entity_display.repository')
->getViewDisplay('node', $content_type, $display)
->getThirdPartySettings('panelizer');
$display_exists = isset($settings['displays'][$id]);
$this
->assertFalse($display_exists);
}
}