You are here

IncidentTest.php in Radioactivity 4.0.x

Same filename and directory in other branches
  1. 8.3 tests/src/Unit/IncidentTest.php

File

tests/src/Unit/IncidentTest.php
View source
<?php

namespace Drupal\Tests\radioactivity\Unit;

use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
use Drupal\radioactivity\Incident;

/**
 * @coversDefaultClass \Drupal\radioactivity\Incident
 * @group radioactivity
 */
class IncidentTest extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();

    // Initiate the Settings singleton used by this test.
    new Settings([
      'hash_salt' => 'liesjeleerdelotjelopen',
    ]);
  }

  /**
   * @covers ::getFieldName
   * @covers ::getEntityTypeId
   * @covers ::getEntityId
   * @covers ::getEnergy
   */
  public function testGetters() {
    $incident = new Incident('field_name', 'entity_type', '99', '0', '10', '1234567890');
    $this
      ->assertEquals($incident
      ->getFieldName(), 'field_name');
    $this
      ->assertEquals($incident
      ->getEntityTypeId(), 'entity_type');
    $this
      ->assertEquals($incident
      ->getEntityId(), '99');
    $this
      ->assertEquals($incident
      ->getTargetId(), '0');
    $this
      ->assertEquals($incident
      ->getEnergy(), '10');
  }

  /**
   * @covers ::createFromPostData
   */
  public function testCreateFromPostData() {
    $incident = Incident::createFromPostData([
      'fn' => 'field_name',
      'et' => 'entity_type',
      'id' => '99',
      'ti' => '0',
      'e' => '10',
      'h' => '1234567890',
    ]);
    $this
      ->assertEquals($incident
      ->getFieldName(), 'field_name');
    $this
      ->assertEquals($incident
      ->getEntityTypeId(), 'entity_type');
    $this
      ->assertEquals($incident
      ->getEntityId(), '99');
    $this
      ->assertEquals($incident
      ->getTargetId(), '0');
    $this
      ->assertEquals($incident
      ->getEnergy(), '10');
  }

  /**
   * @covers ::toJson
   */
  public function testJson() {
    $incident = new Incident('field_name', 'entity_type', '99', '0', '10', '1234567890');
    $this
      ->assertEquals($incident
      ->toJson(), '{"fn":"field_name","et":"entity_type","id":"99","ti":"0","e":"10","h":"4bd2afb1d12a72f3a2fdb01b8fdaf128b8c09efa"}');
  }

  /**
   * @covers ::isValid
   */
  public function testValidHash() {
    $incident = new Incident('field_name', 'entity_type', '99', '0', '10', '1234567890');
    $this
      ->assertFalse($incident
      ->isValid());
    $incident = new Incident('field_name', 'entity_type', '99', '0', '10', '4bd2afb1d12a72f3a2fdb01b8fdaf128b8c09efa');
    $this
      ->assertTrue($incident
      ->isValid());
  }

}

Classes

Namesort descending Description
IncidentTest @coversDefaultClass \Drupal\radioactivity\Incident @group radioactivity