ContactPermissionsTest.php in Contact Permissions 8.2
File
tests/src/Functional/ContactPermissionsTest.php
View source
<?php
namespace Drupal\Tests\contact_permissions\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class ContactPermissionsTest extends BrowserTestBase {
use UserCreationTrait;
public static $modules = [
'contact_permissions_test',
];
protected $defaultTheme = 'stark';
protected $contactable;
protected $nonContactable;
protected $admin;
public function setUp() {
parent::setUp();
$contactable = $this
->drupalCreateUser();
$contactable
->addRole('contactable');
$contactable
->save();
$this->contactable = $contactable;
$noncontactable = $this
->drupalCreateUser();
$noncontactable
->addRole('noncontactable');
$noncontactable
->save();
$this->nonContactable = $noncontactable;
$this->admin = $this
->drupalCreateUser([], NULL, TRUE);
}
public function testContactPermissions() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->admin);
$this
->drupalGet('admin/people/permissions');
$assert
->pageTextContains('Have a personal contact form');
$roles = user_roles(TRUE);
foreach ($roles as $role) {
$label = $role
->label();
$assert
->pageTextContains("Use {$label}'s personal contact forms");
}
$assert
->pageTextNotContains("Use Anonymous user's personal contact forms");
$assert
->checkboxChecked('contactable[have a personal contact form]');
$assert
->checkboxNotChecked('contactable[use noncontactable personal contact forms]');
$assert
->checkboxChecked('noncontactable[use contactable personal contact forms]');
$assert
->checkboxNotChecked('anonymous[have a personal contact form]');
$assert
->checkboxNotChecked('authenticated[have a personal contact form]');
$this
->drupalGet('user/' . $this->contactable
->id() . '/edit');
$assert
->pageTextContains('Contact settings');
$assert
->pageTextContains('Personal contact form');
$assert
->checkboxChecked('contact');
$user = $this
->drupalCreateUser([
'access user contact forms',
]);
$this
->drupalLogin($user);
$this
->drupalGet('user/' . $this->contactable
->id() . '/contact');
$assert
->statusCodeEquals(200);
$this
->drupalGet('user/' . $this->nonContactable
->id() . '/contact');
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this->nonContactable);
$this
->drupalGet('user/' . $this->contactable
->id() . '/contact');
$assert
->statusCodeEquals(200);
$this
->drupalGet('user/' . $this->nonContactable
->id() . '/edit');
$assert
->pageTextNotContains('Contact settings');
$assert
->pageTextNotContains('Personal contact form');
$this
->submitForm([], 'Save', 'user-form');
$assert
->pageTextContains('The changes have been saved.');
$user = $this
->drupalCreateUser();
$user
->addRole('contactable');
$user
->addRole('noncontactable');
$user
->save();
$this
->drupalGet('user/' . $user
->id() . '/contact');
$assert
->statusCodeEquals(200);
$this
->drupalGet('user/' . $this->nonContactable
->id() . '/contact');
$assert
->statusCodeEquals(403);
$this->contactable
->block();
$this->contactable
->save();
$this
->drupalGet('user/' . $this->contactable
->id() . '/contact');
$assert
->statusCodeEquals(403);
$this->contactable
->activate();
$this->contactable
->save();
$this
->drupalLogin($this->contactable);
$edit = [
'contact' => FALSE,
];
$this
->drupalPostForm('user/' . $this->contactable
->id() . '/edit', $edit, 'Save');
$assert
->pageTextContains('The changes have been saved.');
$this
->drupalLogin($this->nonContactable);
$this
->drupalGet('user/' . $this->contactable
->id() . '/contact');
$assert
->statusCodeEquals(403);
}
}