You are here

public function ProductVariationFieldInjectionTest::testInjectedVariationFromUrl in Commerce Core 8.2

Tests that the default injected variation respects the URL context.

File

modules/product/tests/src/Functional/ProductVariationFieldInjectionTest.php, line 127

Class

ProductVariationFieldInjectionTest
Tests the product variation field display injection.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testInjectedVariationFromUrl() {
  $this
    ->drupalGet($this->product
    ->toUrl());

  // We hide the SKU by default.
  $this
    ->assertSession()
    ->pageTextNotContains('INJECTION-CYAN');

  /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $variation_view_display */
  $variation_view_display = commerce_get_entity_display('commerce_product_variation', 'default', 'view');
  $variation_view_display
    ->removeComponent('title');
  $variation_view_display
    ->setComponent('attribute_color', [
    'label' => 'above',
    'type' => 'entity_reference_label',
  ]);
  $variation_view_display
    ->setComponent('sku', [
    'label' => 'hidden',
    'type' => 'string',
  ]);
  $variation_view_display
    ->save();
  $this
    ->drupalGet($this->product
    ->toUrl());
  $this
    ->assertSession()
    ->pageTextContains('INJECTION-CYAN');
  $variations = $this->product
    ->getVariations();
  foreach ($variations as $variation) {
    $this
      ->drupalGet($variation
      ->toUrl());
    $this
      ->assertSession()
      ->pageTextContains($variation
      ->label());
  }
}