View source
<?php
namespace Drupal\Tests\commerce_license\FunctionalJavascript;
use Drupal\Tests\commerce\FunctionalJavascript\CommerceWebDriverTestBase;
use Drupal\user\RoleInterface;
class LicenseAdminTest extends CommerceWebDriverTestBase {
public static $modules = [
'block',
'path',
'commerce_product',
'commerce_license',
];
protected $variation;
protected function getAdministratorPermissions() {
return array_merge([
'access commerce_license overview',
'administer commerce_license',
], parent::getAdministratorPermissions());
}
protected function setUp() : void {
parent::setUp();
$trait_manager = \Drupal::service('plugin.manager.commerce_entity_trait');
$this
->createEntity('commerce_product_variation_type', [
'id' => 'license_pv_type',
'label' => $this
->randomMachineName(),
'orderItemType' => 'default',
'traits' => [
'commerce_license',
],
]);
$trait = $trait_manager
->createInstance('commerce_license');
$trait_manager
->installTrait($trait, 'commerce_product_variation', 'license_pv_type');
$roles = $this->adminUser
->getRoles(TRUE);
$this->variation = $this
->createEntity('commerce_product_variation', [
'type' => 'license_pv_type',
'sku' => $this
->randomMachineName(),
'price' => [
'number' => 5.55,
'currency_code' => 'USD',
],
'license_type' => [
'target_plugin_id' => 'role',
'target_plugin_configuration' => [
'license_role' => reset($roles),
],
],
]);
}
public function testCreateLicense() {
$this
->drupalGet('admin/commerce/licenses');
$this
->getSession()
->getPage()
->clickLink('Add license');
$this
->assertSession()
->fieldExists('product_variation[0][target_id]');
$this
->assertSession()
->fieldExists('uid[0][target_id]');
$this
->assertSession()
->fieldExists('expiration_type[0][target_plugin_id]');
$this
->assertSession()
->fieldExists('license_role');
$this
->getSession()
->getPage()
->selectFieldOption('expiration_type[0][target_plugin_id]', 'rolling_interval');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->getSession()
->getPage()
->fillField('expiration_type[0][target_plugin_configuration][rolling_interval][interval][interval]', 30);
$this
->getSession()
->getPage()
->selectFieldOption('expiration_type[0][target_plugin_configuration][rolling_interval][interval][period]', 'day');
$this
->assertSession()
->optionNotExists('license_role', RoleInterface::AUTHENTICATED_ID);
$this
->assertSession()
->optionNotExists('license_role', RoleInterface::ANONYMOUS_ID);
$this
->getSession()
->getPage()
->fillField('product_variation[0][target_id]', $this->variation
->getSku() . ' (' . $this->variation
->id() . ')');
$roles = $this->adminUser
->getRoles(TRUE);
$this
->getSession()
->getPage()
->selectFieldOption('license_role', reset($roles));
$this
->submitForm([], t('Save'));
$license = $this->container
->get('entity_type.manager')
->getStorage('commerce_license')
->load(1);
$this
->assertEquals('role', $license
->bundle());
$this
->assertEquals($this->variation
->id(), $license
->getPurchasedEntity()
->id());
$this
->assertEquals('new', $license
->getState()
->getId());
$this
->assertEquals('rolling_interval', $license
->getExpirationPluginType());
$this
->assertEquals([
'interval' => [
'interval' => '30',
'period' => 'day',
],
], $license
->get('expiration_type')->target_plugin_configuration);
}
}