You are here

public function CertificateTest::testCertificateMapping in Certificate 4.x

Test that the user receives the correct certificate.

File

tests/src/Functional/CertificateTest.php, line 80

Class

CertificateTest
Tests for Certificate.

Namespace

Drupal\Tests\certificate\Functional

Code

public function testCertificateMapping() {
  $activity = Drupal::entityTypeManager()
    ->getStorage($this->content_type)
    ->create();
  $activity
    ->save();

  // We give them the permission because we have to preview it here.
  $u1 = $this
    ->createUser([
    'administer certificate',
    'view certificate_test_entity',
  ], 'certified2', FALSE, [
    'mail' => 'certified@example.com',
  ]);
  $firstletter = $u1
    ->getAccountName()[0];
  $this
    ->drupalLogin($u1);
  $this
    ->drupalGet("certificate_test_entity/{$activity->id()}/certificate", array(
    'query' => array(
      'certificate_ok' => 1,
      'preview' => TRUE,
    ),
  ));
  $this
    ->assertSession()
    ->statusCodeNotEquals(403, 'Did not get access denied.');
  $this
    ->assertNoText('Custom access denied message.', 'Did not find module provided access denied message on certificate page.');
  $this
    ->assertText('Sorry, there is no certificate available.', 'Found no certificate available text.');

  // Map the first letter of the user's name to the certificate.
  $mapping = CertificateMapping::create([
    'map_key' => 'firstletter',
    'map_value' => $firstletter,
    'cid' => $this->landscape_certificate
      ->id(),
  ]);
  $mapping
    ->save();
  $activity
    ->get('certificate_mapping')
    ->appendItem($mapping
    ->id());
  $activity
    ->save();
  $this
    ->drupalGet("certificate_test_entity/{$activity->id()}/certificate", array(
    'query' => array(
      'certificate_ok' => 1,
      'preview' => TRUE,
    ),
  ));
  $this
    ->assertSession()
    ->statusCodeNotEquals(403, 'Did not get access denied.');
  $this
    ->assertNoText('Custom access denied message.', 'Did not find module provided access denied message on certificate page.');
  $this
    ->assertNoText('Sorry, there is no certificate available.', 'User received certificate.');
  $this
    ->assertText("Landscape certificate body", "Saw certificate body.");
}