View source
<?php
namespace Drupal\Tests\dc_ajax_add_cart\Kernel;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
use Drupal\dc_ajax_add_cart\RefreshPageElementsHelper;
use Drupal\Core\Ajax\AjaxResponse;
class RefreshPageElementsHelperTest extends CommerceKernelTestBase {
public static $modules = [
'block',
'commerce_order',
'dc_ajax_add_cart',
'entity_reference_revisions',
'profile',
'state_machine',
];
protected $controller;
protected $activeTheme;
protected $statusMessagesBlockId;
protected $refreshPageElementsHelper;
protected $expectedAjaxCommandNamesStatusMessagesUpdate = [
'remove',
'insert',
];
protected $expectedAjaxCommandNamesCartBlockUpdate = [
'insert',
];
protected $expectedAjaxCommandNamesFormBuildIdUpdate = [
'update_build_id',
];
protected $expectedAjaxCommandNamesUpdatePageElements = [
'remove',
'insert',
'update_build_id',
];
protected function setUp() {
parent::setUp();
$this->controller = $this->container
->get('entity_type.manager')
->getStorage('block');
$this->activeTheme = $this->container
->get('theme.manager')
->getActiveTheme();
$this->refreshPageElementsHelper = $this->container
->get('dc_ajax_add_cart.refresh_page_elements_helper');
$this->statusMessagesBlockId = $this
->randomMachineName();
}
protected function assertAjaxResponse($response) {
$this
->assertTrue($response instanceof AjaxResponse, 'Ajax response is not returned.');
}
protected function assertInstanceOfRefreshPageElementsHelper($object) {
$this
->assertTrue($object instanceof RefreshPageElementsHelper, 'Not an instance of RefreshPageElementsHelper.');
}
protected function placeStatusMessagesBlock() {
$entity = $this->controller
->create([
'id' => $this->statusMessagesBlockId,
'theme' => $this->activeTheme
->getName(),
'region' => 'content',
'plugin' => 'system_messages_block',
]);
$entity
->save();
}
public function testStatusMessageBlockId() {
$this
->placeStatusMessagesBlock();
$this
->assertEquals($this->refreshPageElementsHelper
->getStatusMessagesBlockId(), $this->statusMessagesBlockId, 'Status messages block is not present.');
}
public function testNoStatusMessageBlockId() {
$this
->assertNull($this->refreshPageElementsHelper
->getStatusMessagesBlockId(), 'Status messages block is present.');
}
public function testAjaxResponseStatusMessagesBlock() {
$this
->placeStatusMessagesBlock();
$refreshPageElements = $this->refreshPageElementsHelper
->updateStatusMessages();
$this
->assertInstanceOfRefreshPageElementsHelper($refreshPageElements);
$response = $refreshPageElements
->getResponse();
$this
->assertAjaxResponse($response);
$ajax_commands = $response
->getCommands();
$actual_ajax_command_names = array_map(function ($i) {
return $i['command'];
}, $ajax_commands);
foreach ($this->expectedAjaxCommandNamesStatusMessagesUpdate as $ajax_command_name) {
$this
->assertTrue(in_array($ajax_command_name, $actual_ajax_command_names), "{$ajax_command_name} is not present");
}
}
public function testAjaxResponseNoStatusMessagesBlock() {
$refreshPageElements = $this->refreshPageElementsHelper
->updateStatusMessages();
$this
->assertInstanceOfRefreshPageElementsHelper($refreshPageElements);
$response = $refreshPageElements
->getResponse();
$this
->assertAjaxResponse($response);
$ajax_commands = $response
->getCommands();
$actual_ajax_command_names = array_map(function ($i) {
return $i['command'];
}, $ajax_commands);
foreach ($this->expectedAjaxCommandNamesStatusMessagesUpdate as $ajax_command_name) {
$this
->assertFalse(in_array($ajax_command_name, $actual_ajax_command_names), "{$ajax_command_name} is present");
}
}
public function testAjaxResponseCartBlock() {
$refreshPageElements = $this->refreshPageElementsHelper
->updateCart();
$this
->assertInstanceOfRefreshPageElementsHelper($refreshPageElements);
$response = $refreshPageElements
->getResponse();
$this
->assertAjaxResponse($response);
$ajax_commands = $response
->getCommands();
$actual_ajax_command_names = array_map(function ($i) {
return $i['command'];
}, $ajax_commands);
foreach ($this->expectedAjaxCommandNamesCartBlockUpdate as $ajax_command_name) {
$this
->assertTrue(in_array($ajax_command_name, $actual_ajax_command_names), "{$ajax_command_name} is not present");
}
}
public function testAjaxResponseFormBuildId() {
$form_build_id_old = $this
->randomMachineName();
$form_build_id = $this
->randomMachineName();
$refreshPageElements = $this->refreshPageElementsHelper
->updateFormBuildId([
'#build_id_old' => $form_build_id_old,
'#build_id' => $form_build_id,
]);
$this
->assertInstanceOfRefreshPageElementsHelper($refreshPageElements);
$response = $refreshPageElements
->getResponse();
$this
->assertAjaxResponse($response);
$ajax_commands = $response
->getCommands();
$actual_ajax_command_names = array_map(function ($i) {
return $i['command'];
}, $ajax_commands);
foreach ($this->expectedAjaxCommandNamesFormBuildIdUpdate as $ajax_command_name) {
$this
->assertTrue(in_array($ajax_command_name, $actual_ajax_command_names), "{$ajax_command_name} is not present");
}
}
public function testNoAjaxResponseFormBuildId() {
$form_build_id_old = $form_build_id = $this
->randomMachineName();
$refreshPageElements = $this->refreshPageElementsHelper
->updateFormBuildId([
'#build_id_old' => $form_build_id_old,
'#build_id' => $form_build_id,
]);
$this
->assertInstanceOfRefreshPageElementsHelper($refreshPageElements);
$response = $refreshPageElements
->getResponse();
$this
->assertAjaxResponse($response);
$ajax_commands = $response
->getCommands();
$actual_ajax_command_names = array_map(function ($i) {
return $i['command'];
}, $ajax_commands);
foreach ($this->expectedAjaxCommandNamesFormBuildIdUpdate as $ajax_command_name) {
$this
->assertFalse(in_array($ajax_command_name, $actual_ajax_command_names), "{$ajax_command_name} is present");
}
}
public function testAjaxResponseUpdatePageElements() {
$this
->placeStatusMessagesBlock();
$form_build_id_old = $this
->randomMachineName();
$form_build_id = $this
->randomMachineName();
$refreshPageElements = $this->refreshPageElementsHelper
->updatePageElements([
'#build_id_old' => $form_build_id_old,
'#build_id' => $form_build_id,
]);
$this
->assertInstanceOfRefreshPageElementsHelper($refreshPageElements);
$response = $refreshPageElements
->getResponse();
$this
->assertAjaxResponse($response);
$ajax_commands = $response
->getCommands();
$actual_ajax_command_names = array_map(function ($i) {
return $i['command'];
}, $ajax_commands);
foreach ($this->expectedAjaxCommandNamesUpdatePageElements as $ajax_command_name) {
$this
->assertTrue(in_array($ajax_command_name, $actual_ajax_command_names), "{$ajax_command_name} is not present");
}
}
}