View source
<?php
namespace Drupal\Tests\entity_legal\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\entity_legal\Traits\EntityLegalTestTrait;
use Drupal\user\Entity\User;
class RedirectMethodTest extends BrowserTestBase {
use AssertMailTrait;
use EntityLegalTestTrait;
protected static $modules = [
'block',
'entity_legal_test',
];
public function _testRedirectMethod() {
$this
->drupalPlaceBlock('system_menu_block:account');
$document = $this
->createDocument(TRUE, TRUE, [
'existing_users' => [
'require_method' => 'redirect',
],
]);
$this
->createDocumentVersion($document, TRUE);
$account = $this
->createUserWithAcceptancePermissions($document);
$this
->drupalLogin($account);
$document_path = $document
->toUrl()
->setAbsolute()
->toString();
$user = \Drupal::entityTypeManager()
->getStorage('user')
->load($account
->id());
$this
->assertUrl($document_path);
$this
->drupalGet('');
$this
->assertUrl($document_path);
$this
->clickLink('Log out');
$this
->assertLink('Log in');
$this
->drupalPostForm(NULL, [
'name' => $account
->getAccountName(),
'pass' => $account->pass_raw,
], 'Log in');
$this
->assertText('You must accept this agreement before continuing.');
$this
->assertText('I agree to the document');
$this
->drupalPostForm(NULL, [
'agree' => TRUE,
], 'Submit');
$this
->drupalGet('');
$this
->assertUrl($user
->toUrl()
->setAbsolute()
->toString());
$this
->clickLink('Log out');
$this
->createDocumentVersion($document, TRUE);
$this
->drupalPostForm(Url::fromRoute('user.pass'), [
'name' => $account
->getAccountName(),
], 'Submit');
$this
->assertText('Further instructions have been sent to your email address.');
$this
->clickMailOneTimeLoginLink();
$this
->assertText("This is a one-time login for {$account->getAccountName()} and will expire on");
$this
->drupalPostForm(NULL, [], 'Log in');
$this
->assertText('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.');
$this
->assertFieldByName('mail');
$this
->assertText('A valid email address. All emails from the system will be sent to this address. The email address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by email.');
$this
->assertFieldByName('pass[pass1]');
$this
->assertFieldByName('pass[pass2]');
$new_password = $this
->randomString();
$account->pass_raw = $new_password;
$this
->drupalPostForm(NULL, [
'pass[pass1]' => $new_password,
'pass[pass2]' => $new_password,
], 'Save');
$this
->assertText('The changes have been saved.');
$this
->assertText('You must accept this agreement before continuing.');
$this
->assertText('I agree to the document');
$this
->drupalPostForm(NULL, [
'agree' => TRUE,
], 'Submit');
$this
->assertFieldByName('mail');
$this
->assertFieldByName('pass[pass1]');
$this
->assertFieldByName('pass[pass2]');
$newest_version = $this
->createDocumentVersion($document, TRUE);
$this
->drupalGet("/user/{$account->id()}/edit", [
'query' => [
'pass-reset-token' => 'arbitrary-invalid-token',
],
]);
$this
->assertText('You must accept this agreement before continuing.');
$this
->assertText('I agree to the document');
$this
->clickLink('Log out');
$this
->drupalGet(Url::fromRoute('system.csrftoken'));
\Drupal::service('module_installer')
->install([
'jsonapi',
]);
$http_client = \Drupal::httpClient();
$url = Url::fromRoute("jsonapi.entity_legal_document_version--{$document->id()}.collection")
->setAbsolute()
->toString();
$response = $http_client
->get($url, [
'headers' => [
'Accept' => 'application/vnd.api+json',
],
]);
$jsonapi = Json::decode($response
->getBody()
->getContents());
$this
->assertTrue(isset($jsonapi['jsonapi']['version']) && isset($jsonapi['data']));
}
public function testMessageDisplay() {
$document = $this
->createDocument(TRUE, TRUE, [
'existing_users' => [
'require_method' => 'redirect',
],
]);
$this
->createDocumentVersion($document, TRUE);
$this
->drupalLogin($this
->createUserWithAcceptancePermissions($document));
$this
->assertSession()
->pageTextContains('You must accept this agreement before continuing.');
$this
->assertSession()
->pageTextNotContains('A status message sample');
$this
->assertSession()
->pageTextNotContains('A warning message sample');
$this
->assertSession()
->pageTextNotContains('An error message sample');
$page = $this
->getSession()
->getPage();
$page
->checkField('I agree to the document');
$page
->pressButton('Submit');
$account = User::load(\Drupal::currentUser()
->id());
$this
->assertUrl($account
->toUrl());
$this
->assertSession()
->pageTextContains('A status message sample');
$this
->assertSession()
->pageTextContains('A warning message sample');
$this
->assertSession()
->pageTextContains('An error message sample');
}
protected function clickMailOneTimeLoginLink() {
$emails = $this
->getMails();
$email = end($emails);
$urls = [];
preg_match('#.+user/reset/.+#', $email['body'], $urls);
$this
->drupalGet($urls[0]);
}
}