You are here

class AgreementTest in Agreement 8.2

Same name and namespace in other branches
  1. 3.0.x tests/src/Unit/Entity/AgreementTest.php \Drupal\Tests\agreement\Unit\Entity\AgreementTest

Tests methods on the agreement entity.

@group agreement

Hierarchy

Expanded class hierarchy of AgreementTest

File

tests/src/Unit/Entity/AgreementTest.php, line 13

Namespace

Drupal\Tests\agreement\Unit\Entity
View source
class AgreementTest extends UnitTestCase {

  /**
   * Default agreement settings from agreement.agreement.default.
   *
   * @todo Add visibility in Drupal 9 when PHP 5 is non-supported.
   *
   * @internal
   */
  const DEFAULT_AGREEMENT_SETTINGS = [
    'visibility' => [
      'settings' => 0,
      'pages' => [],
    ],
    'roles' => [
      'authenticated',
    ],
    'frequency' => -1,
    'title' => 'Our Agreement',
    'checkbox' => 'I agree.',
    'submit' => 'Submit',
    'success' => 'Thank you for accepting our agreement.',
    'revoked' => 'You have successfully revoked your acceptance of our agreement.',
    'failure' => 'You must accept our agreement to continue.',
    'destination' => '',
    'recipient' => '',
    'reset_date' => 0,
    'format' => 'plain_text',
  ];

  /**
   * Asserts that settings are populated.
   *
   * @param array $expected
   *   The expected output.
   * @param array|null $settings
   *   The configuration settings.
   *
   * @dataProvider settingsProvider
   */
  public function testGetSettings(array $expected, $settings) {
    $agreement = new Agreement([
      'id' => 'test',
      'label' => 'Test',
      'path' => '/agreement',
      'agreement' => 'Agree',
      'settings' => $settings,
    ], 'agreement');
    $this
      ->assertArrayEquals($expected, $agreement
      ->getSettings());
  }

  /**
   * Asserts the frequency timestamp based on reset_date and frequency.
   *
   * @param int $lessThan
   *   The expected value to be equal to or less than. This is not the exact
   *   value because it changes by the second.
   * @param array|null $settings
   *   The settings to use.
   *
   * @dataProvider frequencyTimestampProvider
   */
  public function testGetAgreementFrequencyTimestamp($lessThan, $settings) {
    $agreement = new Agreement([
      'id' => 'test',
      'label' => 'Test',
      'path' => '/agreement',
      'agreement' => 'Agree',
      'settings' => $settings,
    ], 'agreement');
    $this
      ->assertLessThanOrEqual($lessThan, $agreement
      ->getAgreementFrequencyTimestamp());
  }

  /**
   * Provides various settings and expected values.
   *
   * @return array
   *   An array of test arguments.
   */
  public function frequencyTimestampProvider() {
    $defaults = Agreement::getDefaultSettings();
    $no_reset_date = self::DEFAULT_AGREEMENT_SETTINGS;
    unset($no_reset_date['reset_date']);
    $frequency_set = self::DEFAULT_AGREEMENT_SETTINGS;
    $frequency_set['frequency'] = 3600;
    return [
      'no settings provided' => [
        0,
        $defaults,
      ],
      'default agreement' => [
        0,
        self::DEFAULT_AGREEMENT_SETTINGS,
      ],
      'no reset date' => [
        0,
        $no_reset_date,
      ],
      'frequency > reset_date' => [
        time(),
        $frequency_set,
      ],
    ];
  }

  /**
   * Provides various settings arrays for tests.
   *
   * @return array
   *   An array of test arguments.
   */
  public function settingsProvider() {
    $defaults = Agreement::getDefaultSettings();
    $no_reset_date = self::DEFAULT_AGREEMENT_SETTINGS;
    unset($no_reset_date['reset_date']);
    return [
      'no settings provided' => [
        $defaults,
        NULL,
      ],
      'empty settings provided' => [
        $defaults,
        [],
      ],
      'default agreement' => [
        self::DEFAULT_AGREEMENT_SETTINGS,
        self::DEFAULT_AGREEMENT_SETTINGS,
      ],
      'no reset date' => [
        self::DEFAULT_AGREEMENT_SETTINGS,
        $no_reset_date,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AgreementTest::DEFAULT_AGREEMENT_SETTINGS constant Default agreement settings from agreement.agreement.default.
AgreementTest::frequencyTimestampProvider public function Provides various settings and expected values.
AgreementTest::settingsProvider public function Provides various settings arrays for tests.
AgreementTest::testGetAgreementFrequencyTimestamp public function Asserts the frequency timestamp based on reset_date and frequency.
AgreementTest::testGetSettings public function Asserts that settings are populated.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340