EventsControllerTest.php in Commerce Google Tag Manager 8.2
File
tests/src/Functional/EventsControllerTest.php
View source
<?php
namespace Drupal\Tests\commerce_google_tag_manager\Functional;
use Drupal\Tests\commerce\Functional\CommerceBrowserTestBase;
use Drupal\Tests\commerce_google_tag_manager\Traits\DeprecationSuppressionTrait;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\Core\Url;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
class EventsControllerTest extends CommerceBrowserTestBase {
use DeprecationSuppressionTrait;
protected $product;
public static $modules = [
'commerce_product',
'google_tag',
'commerce_google_tag_manager',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalLogout();
$user_role = Role::load(RoleInterface::ANONYMOUS_ID);
$user_role
->grantPermission('access content');
$user_role
->save();
$variation = ProductVariation::create([
'type' => 'default',
'sku' => 'lorem-ipsum-120',
'title' => 'Lorem Ipsum',
'price' => new Price('120.00', 'USD'),
'status' => TRUE,
]);
$this->product = Product::create([
'type' => 'default',
'title' => 'Lorem Ipsum',
]);
$this->product
->save();
$this->product
->addVariation($variation)
->save();
}
public function testGetEvents() {
$this
->drupalGet($this->product
->toUrl()
->toString());
$this
->assertSession()
->statusCodeEquals(200);
$url = Url::fromRoute('commerce_google_tag_manager.events');
$content = $this
->drupalGet($url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSame('[{"event":"productDetailViews","ecommerce":{"detail":{"actionField":{"list":""},"products":[{"name":"Lorem Ipsum","id":"1","price":"120.00","variant":"Lorem Ipsum"}]}}}]', (string) $content);
$content = $this
->drupalGet($url);
$this
->assertSame('[]', (string) $content);
}
}