commerce-8.x-2.8/tests/src/Functional/CommerceBrowserTestBase.php
tests/src/Functional/CommerceBrowserTestBase.php
<?php
namespace Drupal\Tests\commerce\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\commerce_store\StoreCreationTrait;
use Drupal\simpletest\BlockCreationTrait;
use Drupal\Tests\BrowserTestBase;
/**
* Provides a base class for Commerce functional tests.
*/
abstract class CommerceBrowserTestBase extends BrowserTestBase {
use BlockCreationTrait;
use StoreCreationTrait;
/**
* The store entity.
*
* @var \Drupal\commerce_store\Entity\Store
*/
protected $store;
/**
* Modules to enable.
*
* Note that when a child class declares its own $modules list, that list
* doesn't override this one, it just extends it.
*
* @see \Drupal\simpletest\WebTestBase::installModulesFromClassProperty()
*
* @var array
*/
public static $modules = [
'system',
'block',
'field',
'commerce',
'commerce_price',
'commerce_store',
];
/**
* A test user with administrative privileges.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->store = $this->createStore();
$this->placeBlock('local_tasks_block');
$this->placeBlock('local_actions_block');
$this->placeBlock('page_title_block');
$this->adminUser = $this->drupalCreateUser($this->getAdministratorPermissions());
$this->drupalLogin($this->adminUser);
}
/**
* Gets the permissions for the admin user.
*
* @return string[]
* The permissions.
*/
protected function getAdministratorPermissions() {
return [
'view the administration theme',
'access administration pages',
'access commerce administration pages',
'administer commerce_currency',
'administer commerce_store',
'administer commerce_store_type',
];
}
/**
* Creates a new entity.
*
* @param string $entity_type
* The entity type to be created.
* @param array $values
* An array of settings.
* Example: 'id' => 'foo'.
*
* @return \Drupal\Core\Entity\EntityInterface
* A new entity.
*/
protected function createEntity($entity_type, array $values) {
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = \Drupal::service('entity_type.manager')->getStorage($entity_type);
$entity = $storage->create($values);
$status = $entity->save();
$this->assertEquals(SAVED_NEW, $status, new FormattableMarkup('Created %label entity %type.', [
'%label' => $entity->getEntityType()->getLabel(),
'%type' => $entity->id(),
]));
// The newly saved entity isn't identical to a loaded one, and would fail
// comparisons.
$entity = $storage->load($entity->id());
return $entity;
}
/**
* Debugger method to save additional HTML output.
*
* The base class will only save browser output when accessing page using
* ::drupalGet and providing a printer class to PHPUnit. This method
* is intended for developers to help debug browser test failures and capture
* more verbose output.
*/
protected function saveHtmlOutput() {
$out = $this->getSession()->getPage()->getContent();
// Ensure that any changes to variables in the other thread are picked up.
$this->refreshVariables();
if ($this->htmlOutputEnabled) {
$html_output = '<hr />Ending URL: ' . $this->getSession()->getCurrentUrl();
$html_output .= '<hr />' . $out;
$html_output .= $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
}
/**
* Asserts that the passed field values are correct.
*
* Ignores differences in ordering.
*
* @param array $field_values
* The field values.
* @param array $expected_values
* The expected values.
* @param string $message
* (optional) A message to display with the assertion. Do not translate
* messages:
* use \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
* to embed variables in the message text, not t().
* If left blank, a default message will be displayed.
*/
protected function assertFieldValues(array $field_values, array $expected_values, $message = '') {
$valid = TRUE;
if (count($field_values) == count($expected_values)) {
foreach ($expected_values as $value) {
if (!in_array($value, $field_values)) {
$valid = FALSE;
break;
}
}
}
else {
$valid = FALSE;
}
$this->assertNotEmpty($valid, $message);
}
}
