You are here

public function ProductRulesEventsTest::testRulesEvents in Ubercart 8.4

Tests the event provided by uc_product.

File

uc_product/tests/src/Functional/ProductRulesEventsTest.php, line 73

Class

ProductRulesEventsTest
Tests the one event that uc_product provides for use in Rules module.

Namespace

Drupal\Tests\uc_product\Functional

Code

public function testRulesEvents() {

  // Create a reaction rules for each event that uc_product triggers.
  $rule_data = [
    1 => [
      'uc_product_load',
      'A product is being loaded',
    ],
  ];
  foreach ($rule_data as $i => list($event_name, $description)) {
    $rule[$i] = $this->expressionManager
      ->createRule();
    $message[$i] = 'RULES message ' . $i . ': ' . $description;
    $rule[$i]
      ->addAction('rules_system_message', ContextConfig::create()
      ->setValue('message', $message[$i])
      ->setValue('type', 'status'));
    $config_entity = $this->rulesStorage
      ->create([
      'id' => 'rule' . $i,
      'events' => [
        [
          'event_name' => $event_name,
        ],
      ],
      'expression' => $rule[$i]
        ->getConfiguration(),
    ]);
    $config_entity
      ->save();
  }

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();
  $product1 = $this
    ->createProduct();

  /** @var \Drupal\node\Entity\NodeType $class */
  $class = $this
    ->createProductClass();
  $product2 = $this
    ->createProduct([
    'type' => $class
      ->id(),
  ]);

  // View product - should trigger the event.
  $this
    ->drupalGet('node/' . $product1
    ->id());
  $assert
    ->pageTextContains($message[1], '"' . $message[1] . '" IS shown');

  // View product class - should also trigger the event.
  $this
    ->drupalGet('node/' . $product2
    ->id());
  $assert
    ->pageTextContains($message[1], '"' . $message[1] . '" IS shown');
}