Permalink
Browse files

Issue #2747909 by smccabe: Convert EntitySelectWidgetTest to BrowserTest

  • Loading branch information...
smmccabe committed Jun 15, 2016
1 parent 3911461 commit 9846bd7d081d364418413a9398a8e2f97a674fe1
Showing with 19 additions and 18 deletions.
  1. +19 −18 {src/Tests → tests/src/Functional}/EntitySelectWidgetTest.php
@@ -1,6 +1,6 @@
<?php <?php
namespace Drupal\commerce\Tests; namespace Drupal\Tests\commerce\Functional;
use Drupal\commerce_product\Entity\Product; use Drupal\commerce_product\Entity\Product;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
@@ -12,7 +12,7 @@
* *
* @group commerce * @group commerce
*/ */
class EntitySelectWidgetTest extends CommerceTestBase { class EntitySelectWidgetTest extends CommerceBrowserTestBase {
use StoreCreationTrait; use StoreCreationTrait;
@@ -93,35 +93,36 @@ public function testWidget() {
$this->createStores(1); $this->createStores(1);
$store_id = $this->stores[0]->id(); $store_id = $this->stores[0]->id();
$this->drupalGet($form_url); $this->drupalGet($form_url);
$this->assertFieldByXpath('//input[@type="hidden" and @name="stores[target_id][value]" and @value="' . $store_id . '"]', NULL, 'Stores field is displayed as a hidden element.'); $field = $this->getSession()->getPage()->find('xpath', '//input[@type="hidden" and @name="stores[target_id][value]" and @value="' . $store_id . '"]');
$this->assertTrue(!empty($field));
// Create another store. The widget should now be a set of checkboxes. // Create another store. The widget should now be a set of checkboxes.
$this->createStores(1); $this->createStores(1);
$store_ids = array_map(function ($store) { $store_ids = array_map(function ($store) {
return $store->id(); return $store->id();
}, $this->stores); }, $this->stores);
$this->drupalGet($form_url); $this->drupalGet($form_url);
$this->assertTrue((bool) $this->xpath('//input[@type="checkbox" and starts-with(@name,"stores")]'), 'Stores field is displayed as a checkboxes element.'); $this->assertTrue((bool) $this->getSession()->getPage()->find('xpath', '//input[@type="checkbox" and starts-with(@name,"stores")]'));
$this->assertNoFieldChecked('edit-stores-target-id-value-1'); $this->assertSession()->checkboxNotChecked('edit-stores-target-id-value-1');
$this->assertNoFieldChecked('edit-stores-target-id-value-2'); $this->assertSession()->checkboxNotChecked('edit-stores-target-id-value-2');
// Check store 1. // Check store 1.
$edit['stores[target_id][value][' . $store_ids[0] . ']'] = $store_ids[0]; $edit['stores[target_id][value][' . $store_ids[0] . ']'] = $store_ids[0];
$edit['stores[target_id][value][' . $store_ids[1] . ']'] = FALSE; $edit['stores[target_id][value][' . $store_ids[1] . ']'] = FALSE;
$this->drupalPostForm(NULL, $edit, t('Save and keep published')); $this->submitForm($edit, t('Save and keep published'));
$this->assertResponse(200); $this->assertSession()->statusCodeEquals(200);
\Drupal::entityTypeManager()->getStorage('commerce_product')->resetCache(); \Drupal::entityTypeManager()->getStorage('commerce_product')->resetCache();
$this->product = Product::load($this->product->id()); $this->product = Product::load($this->product->id());
$this->assertFieldValues($this->product->getStoreIds(), [$store_ids[0]], 'The correct store has been set on the product.'); $this->assertFieldValues($this->product->getStoreIds(), [$store_ids[0]], 'The correct store has been set on the product.');
$this->drupalGet($form_url); $this->drupalGet($form_url);
$this->assertFieldChecked('edit-stores-target-id-value-' . $store_ids[0]); $this->assertSession()->checkboxChecked('edit-stores-target-id-value-' . $store_ids[0]);
$this->assertNoFieldChecked('edit-stores-target-id-value-' . $store_ids[1]); $this->assertSession()->checkboxNotChecked('edit-stores-target-id-value-' . $store_ids[1]);
// Reduce the cardinality to 1. Checkboxes should now be radios. // Reduce the cardinality to 1. Checkboxes should now be radios.
$this->referenceField->setCardinality(1)->save(); $this->referenceField->setCardinality(1)->save();
$this->drupalGet($form_url); $this->drupalGet($form_url);
$this->assertTrue((bool) $this->xpath('//input[@type="radio" and @name="stores[target_id][value]"]'), 'Stores field is displayed as a radio element.'); $this->assertTrue((bool) $this->getSession()->getPage()->find('xpath', '//input[@type="radio" and @name="stores[target_id][value]"]'));
$this->assertFieldChecked('edit-stores-target-id-value-' . $store_ids[0], 'Radio field for store ' . $store_ids[0] . ' is checked.'); $this->assertSession()->checkboxChecked('edit-stores-target-id-value-' . $store_ids[0]);
$this->assertNoFieldChecked('edit-stores-target-id-value-' . $store_ids[1], 'Radio field for store ' . $store_ids[1] . ' is unchecked.'); $this->assertSession()->checkboxNotChecked('edit-stores-target-id-value-' . $store_ids[1]);
// Create the final store. The widget should now be an autocomplete field. // Create the final store. The widget should now be an autocomplete field.
$this->createStores(1); $this->createStores(1);
@@ -130,18 +131,18 @@ public function testWidget() {
}, $this->stores); }, $this->stores);
$this->referenceField->setCardinality(FieldStorageConfig::CARDINALITY_UNLIMITED)->save(); $this->referenceField->setCardinality(FieldStorageConfig::CARDINALITY_UNLIMITED)->save();
$this->drupalGet($form_url); $this->drupalGet($form_url);
$this->assertTrue((bool) $this->xpath('//input[@id="edit-stores-target-id-value" and starts-with(@class, "form-autocomplete")]'), 'Stores field is displayed as an autocomplete element.'); $this->assertTrue((bool) $this->getSession()->getPage()->find('xpath', '//input[@id="edit-stores-target-id-value" and starts-with(@class, "form-autocomplete")]'));
$this->assertFieldByName('stores[target_id][value]', $store_labels[0]); $this->assertSession()->fieldValueEquals('stores[target_id][value]', $store_labels[0]);
// Reference both stores 1 and 2. // Reference both stores 1 and 2.
$edit = []; $edit = [];
$edit['stores[target_id][value]'] = $store_labels[0] . ', ' . $store_labels[1]; $edit['stores[target_id][value]'] = $store_labels[0] . ', ' . $store_labels[1];
$this->drupalPostForm(NULL, $edit, t('Save and keep published')); $this->submitForm($edit, t('Save and keep published'));
$this->assertResponse(200); $this->assertSession()->statusCodeEquals(200);
\Drupal::entityTypeManager()->getStorage('commerce_product')->resetCache(); \Drupal::entityTypeManager()->getStorage('commerce_product')->resetCache();
$this->product = Product::load($this->product->id()); $this->product = Product::load($this->product->id());
$this->assertFieldValues($this->product->getStoreIds(), [$store_ids[0], $store_ids[1]], 'The correct stores have been set on the product.'); $this->assertFieldValues($this->product->getStoreIds(), [$store_ids[0], $store_ids[1]], 'The correct stores have been set on the product.');
$this->drupalGet($form_url); $this->drupalGet($form_url);
$this->assertFieldByName('stores[target_id][value]', $store_labels[0] . ', ' . $store_labels[1]); $this->assertSession()->fieldValueEquals('stores[target_id][value]', $store_labels[0] . ', ' . $store_labels[1]);
} }
/** /**

0 comments on commit 9846bd7

Please sign in to comment.