
| Current Path : /var/www/html/store/web/modules/contrib/commerce/tests/src/Traits/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/store/web/modules/contrib/commerce/tests/src/Traits/CommerceBrowserTestTrait.php |
<?php
namespace Drupal\Tests\commerce\Traits;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a trait for Commerce functional tests.
*/
trait CommerceBrowserTestTrait {
/**
* 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 = $this->container->get('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;
}
/**
* Reloads the entity after clearing the static cache.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to reload.
*
* @return \Drupal\Core\Entity\EntityInterface
* The reloaded entity.
*/
protected function reloadEntity(EntityInterface $entity) {
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = $this->container->get('entity_type.manager')->getStorage($entity->getEntityTypeId());
$storage->resetCache([$entity->id()]);
return $storage->load($entity->id());
}
/**
* 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);
}
}