View source
<?php
namespace Drupal\Tests\panelizer\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\BrowserTestBase;
class PanelizerIpeTest extends BrowserTestBase {
use PanelizerTestTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'node',
'field_ui',
'user',
'panels_ipe',
'panelizer',
];
protected function setUp() {
parent::setUp();
$this
->createContentType([
'type' => $this->content_type,
]);
$this->container
->get('panelizer')
->setPanelizerSettings('node', $this->content_type, 'default', [
'enable' => TRUE,
'allow' => FALSE,
'custom' => TRUE,
'default' => 'default',
]);
$this
->rebuildAll();
}
protected $content_type = 'page';
protected function createAdminUser(array $perms = array()) {
$perms += [
'access administration pages',
'access content',
'administer content types',
'administer nodes',
'create page content',
'edit any page content',
'edit own page content',
'administer node display',
'access panels in-place editing',
];
$this
->verbose('<pre>' . print_r($perms, TRUE) . '</pre>');
return $this
->drupalCreateUser($perms);
}
public function testAdminUser() {
$node = $this
->createTestNode();
$this
->loginUser1();
$this
->drupalGet('node/' . $node
->id());
$this
->assertResponse(200);
$drupalSettings = NULL;
$matches = [];
if (preg_match('@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@', $this
->getRawContent(), $matches)) {
$drupalSettings = Json::decode($matches[1]);
$this
->verbose('<pre>' . print_r($drupalSettings, TRUE) . '</pre>');
}
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertArrayHasKey('panels_ipe', $drupalSettings);
$this
->assertArrayHasKey('regions', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('layout', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('user_permission', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panels_display', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('unsaved', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panelizer', $drupalSettings);
$this
->assertArrayHasKey('entity', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('entity_type_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertArrayHasKey('entity_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertArrayHasKey('user_permission', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('revert', $drupalSettings['panelizer']['user_permission']);
$this
->assertArrayHasKey('save_default', $drupalSettings['panelizer']['user_permission']);
}
}
public function testAdministerPanelizerPermission() {
$node = $this
->createTestNode();
$perms = [
'administer panelizer',
];
$account = $this
->createAdminUser($perms);
$this
->drupalLogin($account);
$this
->drupalGet('node/' . $node
->id());
$this
->assertResponse(200);
$drupalSettings = NULL;
$matches = [];
if (preg_match('@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@', $this
->getRawContent(), $matches)) {
$drupalSettings = Json::decode($matches[1]);
$this
->verbose('<pre>' . print_r($drupalSettings, TRUE) . '</pre>');
}
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertArrayHasKey('panels_ipe', $drupalSettings);
$this
->assertArrayHasKey('regions', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('layout', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('user_permission', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panels_display', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('unsaved', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panelizer', $drupalSettings);
$this
->assertArrayHasKey('entity', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('entity_type_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertArrayHasKey('entity_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertArrayHasKey('user_permission', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('revert', $drupalSettings['panelizer']['user_permission']);
$this
->assertArrayHasKey('save_default', $drupalSettings['panelizer']['user_permission']);
$this
->assertTrue($drupalSettings['panelizer']['user_permission']['revert']);
$this
->assertTrue($drupalSettings['panelizer']['user_permission']['save_default']);
}
}
public function testAdministerEntityContentPermission() {
$node = $this
->createTestNode();
$perms = [
'administer panelizer node page content',
];
$drupalSettings = $this
->setupPermissionTests($perms, $node);
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertArrayHasKey('panels_ipe', $drupalSettings);
$this
->assertArrayHasKey('regions', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('layout', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('user_permission', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panels_display', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('unsaved', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panelizer', $drupalSettings);
$this
->assertArrayHasKey('entity', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('entity_type_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertArrayHasKey('entity_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertArrayHasKey('user_permission', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('revert', $drupalSettings['panelizer']['user_permission']);
$this
->assertArrayHasKey('save_default', $drupalSettings['panelizer']['user_permission']);
}
}
public function testAdministerEntityLayoutPermission() {
$node = $this
->createTestNode();
$perms = [
'administer panelizer node page layout',
];
$drupalSettings = $this
->setupPermissionTests($perms, $node);
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertFalse(isset($drupalSettings['panels_ipe']));
$this
->assertFalse(isset($drupalSettings['panelizer']));
}
$this
->drupalLogout();
$perms = [
'administer panelizer node page layout',
'administer panelizer node page content',
];
$drupalSettings = $this
->setupPermissionTests($perms, $node);
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertArrayHasKey('panels_ipe', $drupalSettings);
$this
->assertArrayHasKey('regions', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('layout', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('user_permission', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panels_display', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('unsaved', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panelizer', $drupalSettings);
$this
->assertArrayHasKey('entity', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('entity_type_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertArrayHasKey('entity_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertArrayHasKey('user_permission', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('revert', $drupalSettings['panelizer']['user_permission']);
$this
->assertArrayHasKey('save_default', $drupalSettings['panelizer']['user_permission']);
$this
->assertFalse($drupalSettings['panelizer']['user_permission']['revert']);
$this
->assertFalse($drupalSettings['panelizer']['user_permission']['save_default']);
}
}
public function testAdministerEntityRevertPermission() {
$node = $this
->createTestNode();
$perms = [
'administer panelizer node page revert',
];
$drupalSettings = $this
->setupPermissionTests($perms, $node);
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertArrayNotHasKey('panels_ipe', $drupalSettings);
$this
->assertArrayNotHasKey('panelizer', $drupalSettings);
}
$this
->drupalLogout();
$perms = [
'administer panelizer node page revert',
'administer panelizer node page content',
];
$drupalSettings = $this
->setupPermissionTests($perms, $node);
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertArrayHasKey('panels_ipe', $drupalSettings);
$this
->assertArrayHasKey('regions', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('layout', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('user_permission', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panels_display', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('unsaved', $drupalSettings['panels_ipe']);
$this
->assertArrayHasKey('panelizer', $drupalSettings);
$this
->assertArrayHasKey('entity', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('entity_type_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertArrayHasKey('entity_id', $drupalSettings['panelizer']['entity']);
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertArrayHasKey('user_permission', $drupalSettings['panelizer']);
$this
->assertArrayHasKey('revert', $drupalSettings['panelizer']['user_permission']);
$this
->assertArrayHasKey('save_default', $drupalSettings['panelizer']['user_permission']);
$this
->assertTrue($drupalSettings['panelizer']['user_permission']['revert']);
$this
->assertFalse($drupalSettings['panelizer']['user_permission']['save_default']);
}
}
protected function setupPermissionTests(array $perms, $node = NULL) {
$account = $this
->createAdminUser($perms);
$this
->drupalLogin($account);
if (empty($node)) {
$node = $this
->createTestNode();
}
$this
->drupalGet('node/' . $node
->id());
$this
->assertResponse(200);
$drupalSettings = NULL;
$matches = [];
if (preg_match('@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@', $this
->getRawContent(), $matches)) {
$drupalSettings = Json::decode($matches[1]);
$this
->verbose('<pre>' . print_r($drupalSettings, TRUE) . '</pre>');
}
return $drupalSettings;
}
}