You are here

public function PromotionTest::testCreatePromotionOnTranslatedAdmin in Commerce Core 8.2

Tests creating a promotion on a non English admin.

File

modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php, line 162

Class

PromotionTest
Tests the admin UI for promotions.

Namespace

Drupal\Tests\commerce_promotion\FunctionalJavascript

Code

public function testCreatePromotionOnTranslatedAdmin() {
  $this
    ->config('system.site')
    ->set('default_langcode', 'fr')
    ->save();

  /** @var \Drupal\locale\StringStorageInterface $storage */
  $storage = $this->container
    ->get('locale.storage');

  // Translate the 'Products' string which is used when building the offer
  // conditions.
  $source_string = $storage
    ->createString([
    'source' => 'Products',
  ])
    ->save();
  $storage
    ->createTranslation([
    'lid' => $source_string->lid,
    'language' => 'fr',
    'translation' => 'Produits',
  ])
    ->save();
  $this->adminUser
    ->set('preferred_langcode', 'fr')
    ->save();
  $this
    ->drupalGet('admin/commerce/promotions');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add promotion');
  $this
    ->assertSession()
    ->fieldExists('name[0][value]');
  $this
    ->assertSession()
    ->fieldExists('display_name[0][value]');
  $name = $this
    ->randomMachineName(8);
  $this
    ->getSession()
    ->getPage()
    ->fillField('name[0][value]', $name);
  $this
    ->getSession()
    ->getPage()
    ->fillField('display_name[0][value]', 'Discount');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('offer[0][target_plugin_id]', 'order_buy_x_get_y');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->fillField('offer[0][target_plugin_configuration][order_buy_x_get_y][offer][percentage]', '100');
  $this
    ->getSession()
    ->getPage()
    ->checkField('offer[0][target_plugin_configuration][order_buy_x_get_y][get][conditions][products][order_item_product][enable]');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->fillField('offer[0][target_plugin_configuration][order_buy_x_get_y][get][conditions][products][order_item_product][configuration][form][products]', $this->product
    ->label() . ' (' . $this->product
    ->id() . ')');
  $this
    ->getSession()
    ->getPage()
    ->checkField('offer[0][target_plugin_configuration][order_buy_x_get_y][get][auto_add]');
  $this
    ->submitForm([], t('Save'));

  /** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
  $promotion = Promotion::load(1);
  $this
    ->assertNotEmpty($promotion
    ->getCreatedTime());
  $this
    ->assertNotEmpty($promotion
    ->getChangedTime());
  $this
    ->assertEquals($name, $promotion
    ->getName());
  $this
    ->assertEquals('Discount', $promotion
    ->getDisplayName());
  $offer_configuration = $promotion
    ->getOffer()
    ->getConfiguration();
  $this
    ->assertEquals('percentage', $offer_configuration['offer_type']);
  $this
    ->assertEquals('1', $offer_configuration['offer_percentage']);
  $this
    ->assertEquals('1', $offer_configuration['buy_quantity']);
  $this
    ->assertNotEmpty($offer_configuration['get_auto_add']);
  $this
    ->assertEquals([
    [
      'plugin' => 'order_item_product',
      'configuration' => [
        'products' => [
          [
            'product' => $this->product
              ->uuid(),
          ],
        ],
      ],
    ],
  ], $offer_configuration['get_conditions']);
}