View source
<?php
namespace Drupal\Tests\commerce_google_tag_manager\Functional;
use Drupal\Tests\commerce\Functional\CommerceBrowserTestBase;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Tests\commerce_google_tag_manager\Traits\DeprecationSuppressionTrait;
class ProductDetailViewsTest extends CommerceBrowserTestBase {
use DeprecationSuppressionTrait;
private $tempStore;
protected $product;
public static $modules = [
'commerce_product',
'google_tag',
'commerce_google_tag_manager',
];
protected function setUp() : void {
parent::setUp();
$this->tempStore = $this->container
->get('tempstore.private')
->get('commerce_google_tag_manager');
$this->product = Product::create([
'type' => 'default',
'title' => 'Lorem Ipsum',
]);
$this->product
->save();
$variation = ProductVariation::create([
'type' => 'default',
'sku' => 'lorem-ipsum-120',
'title' => 'Lorem Ipsum',
'price' => new Price('120.00', 'USD'),
'status' => TRUE,
]);
$this->product
->addVariation($variation)
->save();
}
public function testProductDetailViews() {
$this
->drupalGet($this->product
->toUrl()
->toString());
$this
->assertSession()
->statusCodeEquals(200);
$events = $this->tempStore
->get('events');
$this
->assertSame([
'f8e84d8ee071e2fb885d0dc755dd73ab' => [
'event' => 'productDetailViews',
'ecommerce' => [
'detail' => [
'actionField' => [
'list' => '',
],
'products' => [
0 => [
'name' => 'Lorem Ipsum',
'id' => '1',
'price' => '120.00',
'variant' => 'Lorem Ipsum',
],
],
],
],
],
], $events);
}
public function testMissingDefaultVariation() {
$this->product->variations = [];
$this->product
->save();
$this
->drupalGet($this->product
->toUrl()
->toString());
$this
->assertSession()
->statusCodeEquals(200);
$events = $this->tempStore
->get('events');
$this
->assertNull($events);
}
public function testCachability() {
$normalUser = $this
->drupalCreateUser();
$this
->drupalLogin($normalUser);
$events = $this->tempStore
->get('events');
$this
->assertEmpty($events);
$this
->drupalGet($this->product
->toUrl()
->toString());
$events = $this->tempStore
->get('events');
$this
->assertSame([
'f8e84d8ee071e2fb885d0dc755dd73ab' => [
'event' => 'productDetailViews',
'ecommerce' => [
'detail' => [
'actionField' => [
'list' => '',
],
'products' => [
0 => [
'name' => 'Lorem Ipsum',
'id' => '1',
'price' => '120.00',
'variant' => 'Lorem Ipsum',
],
],
],
],
],
], $events);
$this->tempStore
->set('events', NULL);
$this
->drupalGet($this->product
->toUrl()
->toString());
$events = $this->tempStore
->get('events');
$this
->assertSame([
'f8e84d8ee071e2fb885d0dc755dd73ab' => [
'event' => 'productDetailViews',
'ecommerce' => [
'detail' => [
'actionField' => [
'list' => '',
],
'products' => [
0 => [
'name' => 'Lorem Ipsum',
'id' => '1',
'price' => '120.00',
'variant' => 'Lorem Ipsum',
],
],
],
],
],
], $events);
}
}