From ef048c9109feee778aa99837f7f0c394712a0737 Mon Sep 17 00:00:00 2001 From: Matt Glaman Date: Tue, 29 Dec 2015 14:50:33 -0600 Subject: [PATCH] Issue #2617638 by mglaman, vasike: Create a CommerceTestBase --- modules/order/src/Tests/OrderTestBase.php | 63 +++------- modules/product/src/Tests/ProductTestBase.php | 89 +++----------- modules/store/src/Tests/StoreTestBase.php | 25 +--- modules/tax/src/Tests/TaxTestBase.php | 20 ++-- src/Tests/CommerceTestBase.php | 112 ++++++++++++++++++ src/Tests/EntitySelectWidgetTest.php | 63 +--------- 6 files changed, 152 insertions(+), 220 deletions(-) create mode 100644 src/Tests/CommerceTestBase.php diff --git a/modules/order/src/Tests/OrderTestBase.php b/modules/order/src/Tests/OrderTestBase.php index 18652de1d8..803af0885e 100644 --- a/modules/order/src/Tests/OrderTestBase.php +++ b/modules/order/src/Tests/OrderTestBase.php @@ -9,14 +9,13 @@ use Drupal\commerce_product\Entity\ProductVariation; use Drupal\commerce_store\Entity\Store; -use Drupal\commerce_store\Tests\StoreTestBase; +use Drupal\commerce\Tests\CommerceTestBase; use Drupal\Component\Utility\SafeMarkup; -use Drupal\simpletest\WebTestBase; /** * Defines base class for commerce_order test cases. */ -abstract class OrderTestBase extends WebTestBase { +abstract class OrderTestBase extends CommerceTestBase { /** * The variation to test against @@ -43,31 +42,16 @@ abstract class OrderTestBase extends WebTestBase { 'commerce_order', 'commerce_price', 'inline_entity_form', - 'block' + 'block', ]; /** - * A user with permission to administer orders. - * - * @var \Drupal\user\Entity\User + * {@inheritdoc} */ - protected $adminUser; - protected function setUp() { parent::setUp(); - $this->drupalPlaceBlock('local_tasks_block'); - $this->drupalPlaceBlock('local_actions_block'); - $this->drupalPlaceBlock('page_title_block'); - - $this->adminUser = $this->drupalCreateUser([ - 'administer orders', - 'administer order types', - 'administer line item types', - 'access administration pages', - ]); - - // Create a store + // Create a store. $values = [ 'name' => t('Default store'), 'uid' => 1, @@ -103,36 +87,19 @@ protected function setUp() { 'title' => $this->randomMachineName(), 'variations' => [$this->variation], ]); - - $this->drupalLogin($this->adminUser); } /** - * Creates a new entity - * - * @param string $entity_type - * @param array $values - * An array of settings. - * Example: 'id' => 'foo'. - * - * @return \Drupal\Core\Entity\EntityInterface + * {@inheritdoc} */ - protected function createEntity($entity_type, $values) { - $entity = \Drupal::service('entity_type.manager') - ->getStorage($entity_type) - ->create($values); - $status = $entity->save(); - - $this->assertEqual( - $status, - SAVED_NEW, - SafeMarkup::format('Created %label entity %type.', [ - '%label' => $entity->getEntityType()->getLabel(), - '%type' => $entity->id() - ] - ) - ); - - return $entity; + protected function defaultAdminUserPermissions() { + return [ + 'view the administration theme', + 'configure store', + 'administer orders', + 'administer order types', + 'administer line item types', + ]; } + } diff --git a/modules/product/src/Tests/ProductTestBase.php b/modules/product/src/Tests/ProductTestBase.php index 285914820a..2137d97ac7 100644 --- a/modules/product/src/Tests/ProductTestBase.php +++ b/modules/product/src/Tests/ProductTestBase.php @@ -7,14 +7,13 @@ namespace Drupal\commerce_product\Tests; -use Drupal\Component\Utility\SafeMarkup; -use Drupal\simpletest\WebTestBase; +use Drupal\commerce\Tests\CommerceTestBase; use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait; /** * Defines base class for shortcut test cases. */ -abstract class ProductTestBase extends WebTestBase { +abstract class ProductTestBase extends CommerceTestBase { use EntityReferenceTestTrait; @@ -32,14 +31,9 @@ abstract class ProductTestBase extends WebTestBase { 'field_ui', 'options', 'taxonomy', - 'block' + 'block', ]; - /** - * User with permission to administer products. - */ - protected $adminUser; - /** * The product to test against */ @@ -56,19 +50,6 @@ abstract class ProductTestBase extends WebTestBase { protected function setUp() { parent::setUp(); - $this->drupalPlaceBlock('local_tasks_block'); - $this->drupalPlaceBlock('local_actions_block'); - $this->drupalPlaceBlock('page_title_block'); - - $this->adminUser = $this->drupalCreateUser([ - 'administer products', - 'administer product types', - 'administer commerce_product fields', - 'access administration pages', - 'administer commerce_product_variation fields' - ]); - $this->drupalLogin($this->adminUser); - $store_type = $this->createEntity('commerce_store_type', [ 'id' => strtolower($this->randomMachineName(8)), 'label' => $this->randomMachineName(8), @@ -86,60 +67,18 @@ protected function setUp() { } /** - * Creates a new entity - * - * @param string $entity_type - * The entity type. - * @param array $values - * The values used to create the entity. - * - * @return \Drupal\Core\Entity\EntityInterface - */ - protected function createEntity($entity_type, $values) { - $storage = \Drupal::service('entity_type.manager')->getStorage($entity_type); - $entity = $storage->create($values); - $status = $entity->save(); - $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('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; - } - - /** - * 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 $message - * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed - * variables in the message text, not t(). If left blank, a default message - * will be displayed. + * {@inheritdoc} */ - 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->assertTrue($valid, $message); + protected function defaultAdminUserPermissions() { + return [ + 'view the administration theme', + 'configure store', + 'administer products', + 'administer product types', + 'administer commerce_product fields', + 'access administration pages', + 'administer commerce_product_variation fields', + ]; } } diff --git a/modules/store/src/Tests/StoreTestBase.php b/modules/store/src/Tests/StoreTestBase.php index 1380aa1f43..af11beb45a 100644 --- a/modules/store/src/Tests/StoreTestBase.php +++ b/modules/store/src/Tests/StoreTestBase.php @@ -7,13 +7,12 @@ namespace Drupal\commerce_store\Tests; -use Drupal\Component\Utility\SafeMarkup; -use Drupal\simpletest\WebTestBase; +use Drupal\commerce\Tests\CommerceTestBase; /** * Defines base class for commerce test cases. */ -abstract class StoreTestBase extends WebTestBase { +abstract class StoreTestBase extends CommerceTestBase { /** * Modules to enable. @@ -47,24 +46,4 @@ protected function setUp() { $this->drupalLogin($this->adminUser); } - /** - * Creates a new entity. - * - * @param string $entityType - * 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($entityType, array $values) { - $entity = \Drupal::service('entity_type.manager')->getStorage($entityType)->create($values); - $status = $entity->save(); - $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('Created %label entity %type.', ['%label' => $entity->getEntityType()->getLabel(), '%type' => $entity->id()])); - - return $entity; - } - } diff --git a/modules/tax/src/Tests/TaxTestBase.php b/modules/tax/src/Tests/TaxTestBase.php index adb82c3ce6..dc4c4a1851 100644 --- a/modules/tax/src/Tests/TaxTestBase.php +++ b/modules/tax/src/Tests/TaxTestBase.php @@ -7,12 +7,12 @@ namespace Drupal\commerce_tax\Tests; -use Drupal\simpletest\WebTestBase; +use Drupal\commerce\Tests\CommerceTestBase; /** * Defines the base class for tax test cases. */ -abstract class TaxTestBase extends WebTestBase { +abstract class TaxTestBase extends CommerceTestBase { /** * Modules to enable. @@ -21,21 +21,15 @@ abstract class TaxTestBase extends WebTestBase { */ public static $modules = ['commerce', 'commerce_tax', 'commerce_product']; - /** - * User with permission to administer products. - */ - protected $adminUser; - /** * {@inheritdoc} */ - protected function setUp() { - parent::setUp(); - - $this->adminUser = $this->drupalCreateUser([ + protected function defaultAdminUserPermissions() { + return [ + 'view the administration theme', + 'configure store', 'administer stores', 'access administration pages', - ]); - $this->drupalLogin($this->adminUser); + ]; } } diff --git a/src/Tests/CommerceTestBase.php b/src/Tests/CommerceTestBase.php new file mode 100644 index 0000000000..407bfab45c --- /dev/null +++ b/src/Tests/CommerceTestBase.php @@ -0,0 +1,112 @@ +drupalPlaceBlock('local_tasks_block'); + $this->drupalPlaceBlock('local_actions_block'); + $this->drupalPlaceBlock('page_title_block'); + + $this->adminUser = $this->drupalCreateUser($this->defaultAdminUserPermissions()); + $this->drupalLogin($this->adminUser); + } + + /** + * Returns the permissions for the admin user. + * + * @return array + * Array of permissions. + */ + protected function defaultAdminUserPermissions() { + return [ + 'view the administration theme', + 'configure store', + ]; + } + + /** + * 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) { + $entity = \Drupal::service('entity_type.manager') + ->getStorage($entity_type) + ->create($values); + $status = $entity->save(); + $this->assertEqual($status, SAVED_NEW, t('Created %label entity %type.', ['%label' => $entity->getEntityType()->getLabel(), '%type' => $entity->id()])); + + return $entity; + } + + /** + * 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 $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)) { + $message = 'Counts did not match'; + foreach ($expected_values as $value) { + if (!in_array($value, $field_values)) { + $valid = FALSE; + break; + } + } + } + else { + $valid = FALSE; + } + + $this->assertTrue($valid, $message); + } + +} diff --git a/src/Tests/EntitySelectWidgetTest.php b/src/Tests/EntitySelectWidgetTest.php index 7dd24d9f97..26249c772f 100644 --- a/src/Tests/EntitySelectWidgetTest.php +++ b/src/Tests/EntitySelectWidgetTest.php @@ -9,8 +9,6 @@ use Drupal\commerce_product\Entity\Product; use Drupal\field\Entity\FieldStorageConfig; -use Drupal\simpletest\WebTestBase; -use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Entity\Entity\EntityFormDisplay; /** @@ -18,7 +16,7 @@ * * @group commerce */ -class EntitySelectWidgetTest extends WebTestBase { +class EntitySelectWidgetTest extends CommerceTestBase { /** * Modules to enable. @@ -83,7 +81,7 @@ protected function setUp() { ])->save(); $variation = $this->createEntity('commerce_product_variation', [ - 'type' => 'default', + 'type' => 'default', 'sku' => strtolower($this->randomMachineName()), ]); $this->product = $this->createEntity('commerce_product', [ @@ -171,61 +169,4 @@ protected function createStores($num_stores) { } } - /** - * Creates a new entity - * - * @param string $entityType - * The entity type. - * @param array $values - * The values used to create the entity. - * - * @return \Drupal\Core\Entity\EntityInterface - */ - protected function createEntity($entityType, $values) { - $storage = \Drupal::entityManager()->getStorage($entityType); - $entity = $storage->create($values); - $status = $entity->save(); - $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('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; - } - - /** - * 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 $message - * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() 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->assertTrue($valid, $message); - } - }