You are here

public function CertificateTest::testCertificateGlobalMapping in Certificate 4.x

Test that global mappings correctly populate courses and local overrides are retained.

File

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

Class

CertificateTest
Tests for Certificate.

Namespace

Drupal\Tests\certificate\Functional

Code

public function testCertificateGlobalMapping() {
  $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);

  // Set globals
  $config = \Drupal::configFactory()
    ->getEditable('certificate.settings');
  $config
    ->set('maps', [
    'firstletter' => [
      $firstletter => $this->landscape_certificate
        ->id(),
    ],
  ]);

  // Turn off snapshots because we have to award a different certificate.
  $config
    ->set('snapshots', 0);
  $config
    ->save();
  $this
    ->drupalGet("certificate_test_entity/{$activity->id()}/certificate", array(
    'query' => array(
      'certificate_ok' => 1,
      'preview' => TRUE,
    ),
  ));
  $this
    ->assertText("Landscape certificate body", "Saw certificate body.");

  // Map locally to a different certificate.
  // Map the first letter of the user's name to the certificate.
  $mapping = CertificateMapping::create([
    'map_key' => 'firstletter',
    'map_value' => $firstletter,
    'cid' => $this->portrait_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
    ->assertText('Portrait certificate body', "Saw certificate body.");
}