View source
<?php
namespace Drupal\panelizer\Tests;
use Drupal\Component\Serialization\Json;
use Drupal\simpletest\WebTestBase;
use Drupal\user\Entity\User;
class PanelizerIpeTest extends WebTestBase {
use PanelizerTestTrait;
protected $profile = 'standard';
public static $modules = [
'panelizer',
];
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);
}
protected function loginUser1() {
$account = User::load(1);
$account
->setPassword('foo')
->save();
$account->pass_raw = 'foo';
$this
->drupalLogin($account);
}
public function testAdminUser() {
$this
->setupContentType();
$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->content, $matches)) {
$drupalSettings = Json::decode($matches[1]);
$this
->verbose('<pre>' . print_r($drupalSettings, TRUE) . '</pre>');
}
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertTrue(isset($drupalSettings['panels_ipe']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['regions']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['layout']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['panels_display']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['unsaved']));
$this
->assertTrue(isset($drupalSettings['panelizer']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_type_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['revert']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['save_default']));
}
}
public function testAdministerPanelizer() {
$this
->setupContentType();
$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->content, $matches)) {
$drupalSettings = Json::decode($matches[1]);
$this
->verbose('<pre>' . print_r($drupalSettings, TRUE) . '</pre>');
}
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertTrue(isset($drupalSettings['panels_ipe']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['regions']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['layout']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['panels_display']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['unsaved']));
$this
->assertTrue(isset($drupalSettings['panelizer']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_type_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['revert']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['save_default']));
$this
->assertTrue($drupalSettings['panelizer']['user_permission']['revert']);
$this
->assertTrue($drupalSettings['panelizer']['user_permission']['save_default']);
}
}
public function testAdministerEntityContent() {
$this
->setupContentType();
$node = $this
->createTestNode();
$perms = [
'administer panelizer node page content',
];
$drupalSettings = $this
->setupPermissionTests($perms, $node);
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertTrue(isset($drupalSettings['panels_ipe']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['regions']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['layout']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['panels_display']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['unsaved']));
$this
->assertTrue(isset($drupalSettings['panelizer']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_type_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['revert']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['save_default']));
}
}
public function testAdministerEntityLayout() {
$this
->setupContentType();
$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
->assertTrue(isset($drupalSettings['panels_ipe']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['regions']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['layout']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['panels_display']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['unsaved']));
$this
->assertTrue(isset($drupalSettings['panelizer']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_type_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['revert']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['save_default']));
$this
->assertFalse($drupalSettings['panelizer']['user_permission']['revert']);
$this
->assertFalse($drupalSettings['panelizer']['user_permission']['save_default']);
}
}
public function testAdministerEntityRevert() {
$this
->setupContentType();
$node = $this
->createTestNode();
$perms = [
'administer panelizer node page revert',
];
$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 revert',
'administer panelizer node page content',
];
$drupalSettings = $this
->setupPermissionTests($perms, $node);
$this
->assertNotNull($drupalSettings);
if (!empty($drupalSettings)) {
$this
->assertTrue(isset($drupalSettings['panels_ipe']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['regions']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['layout']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['panels_display']));
$this
->assertTrue(isset($drupalSettings['panels_ipe']['unsaved']));
$this
->assertTrue(isset($drupalSettings['panelizer']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']));
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_type_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_type_id'], 'node');
$this
->assertTrue(isset($drupalSettings['panelizer']['entity']['entity_id']));
$this
->assertEqual($drupalSettings['panelizer']['entity']['entity_id'], $node
->id());
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['revert']));
$this
->assertTrue(isset($drupalSettings['panelizer']['user_permission']['save_default']));
$this
->assertTrue($drupalSettings['panelizer']['user_permission']['revert']);
$this
->assertFalse($drupalSettings['panelizer']['user_permission']['save_default']);
}
}
protected function setupContentType() {
$this
->loginUser1();
$this
->panelize($this->content_type, NULL, [
'panelizer[custom]' => TRUE,
]);
$this
->drupalLogout();
}
protected function createTestNode() {
return $this
->drupalCreateNode([
'title' => t('Hello, world!'),
'type' => $this->content_type,
]);
}
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->content, $matches)) {
$drupalSettings = Json::decode($matches[1]);
$this
->verbose('<pre>' . print_r($drupalSettings, TRUE) . '</pre>');
}
return $drupalSettings;
}
}