class ReactionRuleConfigTest

@coversDefaultClass \Drupal\rules\Entity\ReactionRuleConfig
@group Rules

Hierarchy

Expanded class hierarchy of ReactionRuleConfigTest

File

tests/src/Unit/Entity/ReactionRuleConfigTest.php, line 12

Namespace

Drupal\Tests\rules\Unit\Entity
View source
class ReactionRuleConfigTest extends RulesUnitTestBase {
  
  /**
   * Creates a rule.
   *
   * @param array $values
   *   (optional) An array of values to set, keyed by property name.
   */
  protected function createRule(array $values = []) {
    $values += [
      'id' => 'test_rule',
    ];
    return new ReactionRuleConfig($values, 'rules_reaction_rule');
  }
  
  /**
   * @covers ::getEvents
   */
  public function testGetEvents() {
    // Create a rule with a few events.
    $rule = $this->createRule([
      'events' => [
        [
          'event_name' => 'foo',
        ],
        [
          'event_name' => 'bar',
          'configuration' => [
            'qux' => 'baz',
          ],
        ],
      ],
    ]);
    $expected = [
      [
        'event_name' => 'foo',
      ],
      [
        'event_name' => 'bar',
        'configuration' => [
          'qux' => 'baz',
        ],
      ],
    ];
    $this->assertSame($expected, $rule->getEvents());
  }
  
  /**
   * @covers ::getEventNames
   */
  public function testGetEventNames() {
    // Create a rule with a few events.
    $rule = $this->createRule([
      'events' => [
        [
          'event_name' => 'foo',
        ],
        [
          'event_name' => 'bar',
          'configuration' => [
            'qux' => 'baz',
          ],
        ],
      ],
    ]);
    $expected = [
      'foo',
      'bar',
    ];
    $this->assertSame($expected, $rule->getEventNames());
  }
  
  /**
   * @covers ::addEvent
   * @covers ::getEvents
   *
   * @dataProvider addEventDataProvider
   */
  public function testAddEvent(array $expected, array $events_init, array $event_add) {
    $rule = $this->createRule([
      'events' => $events_init,
    ]);
    if (isset($event_add['configuration'])) {
      $this->assertSame($rule, $rule->addEvent($event_add['event_name'], $event_add['configuration']));
    }
    else {
      $this->assertSame($rule, $rule->addEvent($event_add['event_name']));
    }
    $this->assertSame($expected, $rule->getEvents());
  }
  
  /**
   * Data provider for ::testAddEvent().
   */
  public function addEventDataProvider() {
    return [
      'no events' => [
        'expected' => [
          [
            'event_name' => 'foo',
          ],
        ],
        'events_init' => [],
        'event_add' => [
          'event_name' => 'foo',
        ],
      ],
      'single event' => [
        'expected' => [
          [
            'event_name' => 'foo',
          ],
          [
            'event_name' => 'bar',
          ],
        ],
        'events_init' => [
          [
            'event_name' => 'foo',
          ],
        ],
        'event_add' => [
          'event_name' => 'bar',
        ],
      ],
      'with config' => [
        'expected' => [
          [
            'event_name' => 'foo',
          ],
          [
            'event_name' => 'bar',
            'configuration' => [
              'qux' => 'baz',
            ],
          ],
        ],
        'events_init' => [
          [
            'event_name' => 'foo',
          ],
        ],
        'event_add' => [
          'event_name' => 'bar',
          'configuration' => [
            'qux' => 'baz',
          ],
        ],
      ],
      'duplicate event' => [
        'expected' => [
          [
            'event_name' => 'foo',
          ],
        ],
        'events_init' => [
          [
            'event_name' => 'foo',
          ],
        ],
        'event_add' => [
          'event_name' => 'foo',
        ],
      ],
    ];
  }
  
  /**
   * @covers ::hasEvent
   */
  public function testHasEvent() {
    // Create a rule with a few events.
    $rule = $this->createRule([
      'events' => [
        [
          'event_name' => 'foo',
        ],
        [
          'event_name' => 'bar',
          'configuration' => [
            'qux' => 'baz',
          ],
        ],
      ],
    ]);
    $this->assertTrue($rule->hasEvent('foo'));
    $this->assertTrue($rule->hasEvent('bar'));
    $this->assertFalse($rule->hasEvent('qux'));
    $this->assertFalse($rule->hasEvent('baz'));
  }
  
  /**
   * @covers ::removeEvent
   * @covers ::getEvents
   */
  public function testRemoveEvent() {
    // Create a rule with a few events.
    $rule = $this->createRule([
      'events' => [
        [
          'event_name' => 'foo',
        ],
        [
          'event_name' => 'bar',
          'configuration' => [
            'qux' => 'baz',
          ],
        ],
      ],
    ]);
    $this->assertSame($rule, $rule->removeEvent('bar'));
    $this->assertSame([
      [
        'event_name' => 'foo',
      ],
    ], $rule->getEvents());
  }
  
  /**
   * @covers ::removeEvent
   * @covers ::getEvents
   */
  public function testRemoveEventWithKeyedIndex() {
    // Create a rule with a few events that are numerically indexed.
    // This situation should not ever happen - the configuration entity
    // expects that events are numerically indexed and that the indices
    // are sequential, starting with 0.
    $rule = $this->createRule([
      'events' => [
        2 => [
          'event_name' => 'foo',
        ],
        3 => [
          'event_name' => 'bar',
          'configuration' => [
            'qux' => 'baz',
          ],
        ],
      ],
    ]);
    $this->assertSame($rule, $rule->removeEvent('foo'));
    // Removing an event should re-index the events so they are sequential
    // and start with 0.
    $expected = [
      0 => [
        'event_name' => 'bar',
        'configuration' => [
          'qux' => 'baz',
        ],
      ],
    ];
    $this->assertSame($expected, $rule->getEvents());
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
ReactionRuleConfigTest::addEventDataProvider public function Data provider for ::testAddEvent().
ReactionRuleConfigTest::createRule protected function Creates a rule.
ReactionRuleConfigTest::testAddEvent public function @covers ::addEvent[[api-linebreak]]
@covers ::getEvents[[api-linebreak]]
ReactionRuleConfigTest::testGetEventNames public function @covers ::getEventNames[[api-linebreak]]
ReactionRuleConfigTest::testGetEvents public function @covers ::getEvents[[api-linebreak]]
ReactionRuleConfigTest::testHasEvent public function @covers ::hasEvent[[api-linebreak]]
ReactionRuleConfigTest::testRemoveEvent public function @covers ::removeEvent[[api-linebreak]]
@covers ::getEvents[[api-linebreak]]
ReactionRuleConfigTest::testRemoveEventWithKeyedIndex public function @covers ::removeEvent[[api-linebreak]]
@covers ::getEvents[[api-linebreak]]
RulesUnitTestBase::$expressionManager protected property The mocked expression manager object. 1
RulesUnitTestBase::$falseConditionExpression protected property A mocked condition that always evaluates to FALSE.
RulesUnitTestBase::$rulesDebugLogger protected property The mocked expression manager object.
RulesUnitTestBase::$testActionExpression protected property A mocked dummy action object.
RulesUnitTestBase::$testFirstActionExpression protected property A mocked dummy action object.
RulesUnitTestBase::$trueConditionExpression protected property A mocked condition that always evaluates to TRUE.
RulesUnitTestBase::setUp protected function Overrides UnitTestCase::setUp 4
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::setUpBeforeClass public static function