View source
<?php
namespace Drupal\entity_print\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Session\AccountInterface;
class EntityPrintTest extends WebTestBase {
public static $modules = [
'node',
'entity_print_test',
];
protected $node;
protected function setUp() {
parent::setUp();
user_role_revoke_permissions(AccountInterface::ANONYMOUS_ROLE, [
'access content',
]);
user_role_revoke_permissions(AccountInterface::AUTHENTICATED_ROLE, [
'access content',
]);
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Page',
));
$this->node = $this
->drupalCreateNode();
$theme = 'entity_print_test_theme';
\Drupal::service('theme_handler')
->install([
$theme,
]);
$this
->config('system.theme')
->set('default', $theme)
->save();
}
public function testEntityPrintThemeCss() {
$account = $this
->drupalCreateUser([
'bypass entity print access',
'access content',
], $this
->randomMachineName());
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/node/' . $this->node
->id() . '/debug');
$config = \Drupal::configFactory()
->getEditable('entity_print.settings');
$this
->assertRaw('entity-print.css');
$config
->set('default_css', FALSE)
->save();
$this
->drupalGet($this
->getUrl());
$this
->assertNoRaw('entity-print.css');
$this
->assertRaw('entityprint-all.css');
$this
->assertRaw('entityprint-page.css');
$this
->assertRaw('entityprint-node.css');
$this
->assertRaw('entityprint-module.css');
}
public function testEntityPrintAccess() {
$account = $this
->drupalCreateUser(array(
'bypass entity print access',
));
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/node/' . $this->node
->id() . '/debug');
$this
->assertResponse(403, 'User with only the bypass entity print access permission cannot view PDF.');
$account = $this
->drupalCreateUser([
'access content',
], $this
->randomMachineName());
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/node/' . $this->node
->id() . '/debug');
$this
->assertResponse(403, 'User with access content but no entity print permission cannot view PDF.');
$account = $this
->drupalCreateUser(array(
'bypass entity print access',
'access content',
));
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/node/' . $this->node
->id() . '/debug');
$this
->assertResponse(200, 'User with both permissions can view the PDF.');
$account = $this
->drupalCreateUser(array(
'entity print access type node',
'access content',
));
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/node/' . $this->node
->id() . '/debug');
$this
->assertResponse(200, 'User with entity print type and access content permission is allowed to see the content.');
$account = $this
->drupalCreateUser(array(
'entity print access type user',
'access content',
));
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/node/' . $this->node
->id() . '/debug');
$this
->assertResponse(403, 'User with different entity print type and access content permission is not allowed to see the content.');
$account = $this
->drupalCreateUser(array(
'entity print access bundle page',
'access content',
));
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/node/' . $this->node
->id() . '/debug');
$this
->assertResponse(200, 'User with entity print bundle and access content permission is allowed to see the content.');
$account = $this
->drupalCreateUser(array(
'entity print access bundle user',
'access content',
));
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/node/' . $this->node
->id() . '/debug');
$this
->assertResponse(403, 'User with different entity print bundle and access content permission is not allowed to see the content.');
$account = $this
->drupalCreateUser(array(
'entity print access bundle user',
'access content',
));
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/user/' . $account
->id() . '/debug');
$this
->assertResponse(200, 'User with entity print user bundle permission and access content permission is allowed to see the content.');
$account = $this
->drupalCreateUser();
$this
->drupalLogin($account);
$this
->drupalGet('entityprint/node/' . $this->node
->id() . '/debug');
$this
->assertResponse(403, 'User with neither permission cannot view the PDF.');
$this
->drupalGet('entityprint/invalid/' . $this->node
->id() . '/debug');
$this
->assertResponse(403, 'Invalid entity type triggers access denied.');
$this
->drupalGet('entityprint/node/invalid-entity-id/debug');
$this
->assertResponse(403, 'Invalid entity id triggers access denied.');
}
}