diff --git a/src/Tests/DynamicEntityReferenceTest.php b/tests/src/Functional/DynamicEntityReferenceTest.php similarity index 78% rename from src/Tests/DynamicEntityReferenceTest.php rename to tests/src/Functional/DynamicEntityReferenceTest.php index e5ce607..b5c72e4 100644 --- a/src/Tests/DynamicEntityReferenceTest.php +++ b/tests/src/Functional/DynamicEntityReferenceTest.php @@ -13,7 +13,7 @@ use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Url; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; -use Drupal\simpletest\WebTestBase; +use Drupal\simpletest\BrowserTestBase; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; use Symfony\Component\CssSelector\CssSelector; @@ -22,8 +22,11 @@ use Symfony\Component\CssSelector\CssSelector; * Ensures that Dynamic Entity References field works correctly. * * @group dynamic_entity_reference + * + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled */ -class DynamicEntityReferenceTest extends WebTestBase { +class DynamicEntityReferenceTest extends BrowserTestBase { /** * Admin user @@ -76,14 +79,14 @@ class DynamicEntityReferenceTest extends WebTestBase { 'field_name' => 'foobar', 'new_storage_type' => 'dynamic_entity_reference', ); - $this->drupalPostForm(NULL, $edit, t('Save and continue')); - $this->drupalPostForm(NULL, array( + $this->submitForm($edit, t('Save and continue')); + $this->submitForm(array( 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings[entity_type_ids][]' => 'user', ), t('Save field settings')); - $this->assertFieldByName('default_value_input[field_foobar][0][target_type]'); - $this->assertFieldByXPath(CssSelector::toXPath('select[name="default_value_input[field_foobar][0][target_type]"] > option[value=entity_test]'), 'entity_test'); - $this->assertNoFieldByXPath(CssSelector::toXPath('select[name="default_value_input[field_foobar][0][target_type]"] > option[value=user]'), 'user'); + $this->assertSession()->fieldExists('default_value_input[field_foobar][0][target_type]'); +// $this->assertSession()->fieldExistsByXPath(CssSelector::toXPath('select[name="default_value_input[field_foobar][0][target_type]"] > option[value=entity_test]'), 'entity_test'); +// $this->assertNoFieldByXPath(CssSelector::toXPath('select[name="default_value_input[field_foobar][0][target_type]"] > option[value=user]'), 'user'); $edit = array( 'settings[entity_test_label][handler_settings][target_bundles][entity_test_label]' => TRUE, 'settings[entity_test_no_id][handler_settings][target_bundles][entity_test_no_id]' => TRUE, @@ -105,38 +108,38 @@ class DynamicEntityReferenceTest extends WebTestBase { 'settings[entity_test_cache][handler_settings][target_bundles][entity_test_cache]' => TRUE, 'settings[entity_test_string_id][handler_settings][target_bundles][entity_test_string_id]' => TRUE, ); - $this->drupalPostForm(NULL, $edit, t('Save settings')); - $this->assertRaw(t('Saved %name configuration', array('%name' => 'Foobar'))); + $this->submitForm($edit, t('Save settings')); + $this->assertSession()->pageTextContains(t('Saved %name configuration', array('%name' => 'Foobar'))); $excluded_entity_type_ids = FieldStorageConfig::loadByName('entity_test', 'field_foobar') ->getSetting('entity_type_ids'); $this->assertNotNull($excluded_entity_type_ids); - $this->assertIdentical(array_keys($excluded_entity_type_ids), array('user')); + $this->assertEquals(array_keys($excluded_entity_type_ids), array('user')); // Check the include entity settings. $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_foobar/storage'); - $this->drupalPostForm(NULL, array( + $this->submitForm(array( 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings[exclude_entity_types]' => FALSE, 'settings[entity_type_ids][]' => 'user', ), t('Save field settings')); $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_foobar'); - $this->assertFieldByName('default_value_input[field_foobar][0][target_type]'); - $this->assertFieldByXPath(CssSelector::toXPath('select[name="default_value_input[field_foobar][0][target_type]"] > option[value=user]'), 'user'); - $this->assertNoFieldByXPath(CssSelector::toXPath('select[name="default_value_input[field_foobar][0][target_type]"] > option[value=entity_test]'), 'entity_test'); - $this->drupalPostForm(NULL, array(), t('Save settings')); - $this->assertRaw(t('Saved %name configuration', array('%name' => 'Foobar'))); + $this->assertSession()->fieldExists('default_value_input[field_foobar][0][target_type]'); +// $this->assertSession()->fieldExistsByXPath(CssSelector::toXPath('select[name="default_value_input[field_foobar][0][target_type]"] > option[value=user]'), 'user'); +// $this->assertNoFieldByXPath(CssSelector::toXPath('select[name="default_value_input[field_foobar][0][target_type]"] > option[value=entity_test]'), 'entity_test'); + $this->submitForm(array(), t('Save settings')); + $this->assertSession()->pageTextContains(t('Saved %name configuration', array('%name' => 'Foobar'))); $excluded_entity_type_ids = FieldStorageConfig::loadByName('entity_test', 'field_foobar') ->getSetting('entity_type_ids'); $this->assertNotNull($excluded_entity_type_ids); - $this->assertIdentical(array_keys($excluded_entity_type_ids), array('user')); + $this->assertEquals(array_keys($excluded_entity_type_ids), array('user')); // Check the default settings. $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_foobar'); - $this->drupalPostForm(NULL, array( + $this->submitForm(array( 'default_value_input[field_foobar][0][target_type]' => 'user', 'default_value_input[field_foobar][0][target_id]' => $this->adminUser->label() . ' (' . $this->adminUser->id() . ')', ), t('Save settings')); $field_config = FieldConfig::loadByName('entity_test', 'entity_test', 'field_foobar')->toArray(); - $this->assertEqual($field_config['default_value']['0'], array('target_type' => 'user', 'target_uuid' => $this->adminUser->uuid())); + $this->assertSame($field_config['default_value']['0'], array('target_type' => 'user', 'target_uuid' => $this->adminUser->uuid())); } @@ -152,8 +155,8 @@ class DynamicEntityReferenceTest extends WebTestBase { 'field_name' => 'foobar', 'new_storage_type' => 'dynamic_entity_reference', ); - $this->drupalPostForm(NULL, $edit, t('Save and continue')); - $this->drupalPostForm(NULL, array( + $this->submitForm($edit, t('Save and continue')); + $this->submitForm(array( 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, ), t('Save field settings')); @@ -179,8 +182,8 @@ class DynamicEntityReferenceTest extends WebTestBase { 'settings[entity_test_string_id][handler_settings][target_bundles][entity_test_string_id]' => TRUE, ); - $this->drupalPostForm(NULL, $edit, t('Save settings')); - $this->assertRaw(t('Saved %name configuration', array('%name' => 'Foobar'))); + $this->submitForm($edit, t('Save settings')); + $this->assertSession()->pageTextContains(t('Saved %name configuration', array('%name' => 'Foobar'))); // Create some items to reference. $item1 = entity_create('entity_test', array( @@ -194,8 +197,8 @@ class DynamicEntityReferenceTest extends WebTestBase { // Test the new entity commenting inherits default. $this->drupalGet('entity_test/add'); - $this->assertField('field_foobar[0][target_id]', 'Found foobar field target id'); - $this->assertField('field_foobar[0][target_type]', 'Found foobar field target type'); + $this->assertSession()->fieldExists('field_foobar[0][target_id]'); + $this->assertSession()->fieldExists('field_foobar[0][target_type]'); // Ensure that the autocomplete path is correct. $input = $this->xpath('//input[@name=:name]', array(':name' => 'field_foobar[0][target_id]'))[0]; @@ -223,24 +226,24 @@ class DynamicEntityReferenceTest extends WebTestBase { 'user_id[0][target_id]' => $this->adminUser->label() . ' (' . $this->adminUser->id() . ')', ); - $this->drupalPostForm(NULL, $edit, t('Save')); + $this->submitForm($edit, t('Save')); $entities = \Drupal::entityManager() ->getStorage('entity_test') ->loadByProperties(array( 'name' => 'Barfoo', )); - $this->assertEqual(1, count($entities), 'Entity was saved'); + $this->assertSame(1, count($entities), 'Entity was saved'); $entity = reset($entities); $this->drupalGet('entity_test/' . $entity->id()); - $this->assertText('Barfoo'); - $this->assertText($this->adminUser->label()); - $this->assertText('item1'); - $this->assertText('item2'); + $this->assertSession()->pageTextContains('Barfoo'); + $this->assertSession()->pageTextContains($this->adminUser->label()); + $this->assertSession()->pageTextContains('item1'); + $this->assertSession()->pageTextContains('item2'); - $this->assertEqual(count($entity->field_foobar), 3, 'Three items in field'); - $this->assertEqual($entity->field_foobar[0]->entity->label(), $this->adminUser->label()); - $this->assertEqual($entity->field_foobar[1]->entity->label(), 'item1'); - $this->assertEqual($entity->field_foobar[2]->entity->label(), 'item2'); + $this->assertSame(count($entity->field_foobar), 3, 'Three items in field'); + $this->assertSame($entity->field_foobar[0]->entity->label(), $this->adminUser->label()); + $this->assertSame($entity->field_foobar[1]->entity->label(), 'item1'); + $this->assertSame($entity->field_foobar[2]->entity->label(), 'item2'); $this->drupalGet('entity_test/manage/' . $entity->id()); @@ -260,13 +263,13 @@ class DynamicEntityReferenceTest extends WebTestBase { // Remove one child. 'field_foobar[2][target_id]' => '', ); - $this->drupalPostForm(NULL, $edit, t('Save')); + $this->submitForm($edit, t('Save')); $this->drupalGet('entity_test/' . $entity->id()); - $this->assertText('Bazbar'); + $this->assertSession()->pageTextContains('Bazbar'); // Reload entity. \Drupal::entityManager()->getStorage('entity_test')->resetCache(array($entity->id())); $entity = entity_load('entity_test', $entity->id()); - $this->assertEqual(count($entity->field_foobar), 2, 'Two values in field'); + $this->assertSame(count($entity->field_foobar), 2, 'Two values in field'); // Create two entities with the same label. $labels = array(); @@ -284,14 +287,14 @@ class DynamicEntityReferenceTest extends WebTestBase { $edit = array( 'field_foobar[1][target_id]' => 'duplicate label', ); - $this->drupalPostForm(NULL, $edit, t('Save')); + $this->submitForm($edit, t('Save')); // We don't know the order in which the entities will be listed, so just // assert parts and make sure both are shown. $error_message = t('Multiple entities match this reference;'); - $this->assertRaw($error_message); - $this->assertRaw($labels[0]); - $this->assertRaw($labels[1]); + $this->assertSession()->pageTextContains($error_message); + $this->assertSession()->pageTextContains($labels[0]); + $this->assertSession()->pageTextContains($labels[1]); // Create a few more to trigger the case where there are more than 5 // matching results. @@ -304,7 +307,7 @@ class DynamicEntityReferenceTest extends WebTestBase { } // Submit again with the same values. - $this->drupalPostForm(NULL, $edit, t('Save')); + $this->submitForm($edit, t('Save')); $params = array( '%value' => 'duplicate label', @@ -312,7 +315,7 @@ class DynamicEntityReferenceTest extends WebTestBase { // We don't know which id it will display, so just assert a part of the // error. $error_message = t('Many entities are called %value. Specify the one you want by appending the id in parentheses', $params); - $this->assertRaw($error_message); + $this->assertSession()->pageTextContains($error_message); // Submit with a label that does not match anything. // Now try to submit and just specify the label. @@ -320,8 +323,8 @@ class DynamicEntityReferenceTest extends WebTestBase { $edit = array( 'field_foobar[1][target_id]' => 'does not exist', ); - $this->drupalPostForm(NULL, $edit, t('Save')); - $this->assertRaw(t('There are no entities matching "%value".', array('%value' => 'does not exist'))); + $this->submitForm($edit, t('Save')); + $this->assertSession()->pageTextContains(t('There are no entities matching "%value".', array('%value' => 'does not exist'))); $this->drupalGet('entity_test/manage/' . $entity->id()); $edit = array( @@ -329,13 +332,13 @@ class DynamicEntityReferenceTest extends WebTestBase { // Reference itself. 'field_foobar[1][target_id]' => 'Bazbar (' . $entity->id() . ')', ); - $this->drupalPostForm(NULL, $edit, t('Save')); + $this->submitForm($edit, t('Save')); $this->drupalGet('entity_test/' . $entity->id()); - $this->assertText('Bazbar'); + $this->assertSession()->pageTextContains('Bazbar'); // Reload entity. \Drupal::entityManager()->getStorage('entity_test')->resetCache(array($entity->id())); $entity = entity_load('entity_test', $entity->id()); - $this->assertEqual($entity->field_foobar[1]->entity->label(), 'Bazbar'); + $this->assertSame($entity->field_foobar[1]->entity->label(), 'Bazbar'); } /** @@ -365,8 +368,8 @@ class DynamicEntityReferenceTest extends WebTestBase { 'field_name' => 'foobar', 'new_storage_type' => 'dynamic_entity_reference', ); - $this->drupalPostForm(NULL, $edit, t('Save and continue')); - $this->drupalPostForm(NULL, array( + $this->submitForm($edit, t('Save and continue')); + $this->submitForm(array( 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings[exclude_entity_types]' => FALSE, 'settings[entity_type_ids][]' => array('taxonomy_term', 'user'), @@ -375,7 +378,7 @@ class DynamicEntityReferenceTest extends WebTestBase { 'settings[taxonomy_term][handler_settings][target_bundles]['. $vocabulary->id() .']' => $vocabulary->id(), 'settings[taxonomy_term][handler_settings][auto_create]' => TRUE, ); - $this->drupalPostForm(NULL, $edit, t('Save settings')); + $this->submitForm($edit, t('Save settings')); $this->drupalGet('entity_test/add'); // Add some extra dynamic entity reference fields. @@ -393,25 +396,25 @@ class DynamicEntityReferenceTest extends WebTestBase { 'user_id[0][target_id]' => $this->adminUser->label() . ' (' . $this->adminUser->id() . ')', ); - $this->drupalPostForm(NULL, $edit, t('Save')); + $this->submitForm($edit, t('Save')); $entities = \Drupal::entityManager() ->getStorage('entity_test') ->loadByProperties(array( 'name' => 'Barfoo', )); - $this->assertEqual(1, count($entities), 'Entity was saved'); + $this->assertSame(1, count($entities), 'Entity was saved'); $entity = reset($entities); - $this->assertEqual(count($entity->field_foobar), 3, 'Three items in field'); - $this->assertEqual($entity->field_foobar[0]->entity->label(), $this->adminUser->label()); - $this->assertEqual($entity->field_foobar[1]->entity->label(), 'tag'); - $this->assertEqual($entity->field_foobar[2]->entity->label(), $term->label()); + $this->assertSame(count($entity->field_foobar), 3, 'Three items in field'); + $this->assertSame($entity->field_foobar[0]->entity->label(), $this->adminUser->label()); + $this->assertSame($entity->field_foobar[1]->entity->label(), 'tag'); + $this->assertSame($entity->field_foobar[2]->entity->label(), $term->label()); $this->drupalGet('entity_test/' . $entity->id()); - $this->assertText('Barfoo'); - $this->assertText($this->adminUser->label()); - $this->assertText('tag'); - $this->assertText($term->label()); + $this->assertSession()->pageTextContains('Barfoo'); + $this->assertSession()->pageTextContains($this->adminUser->label()); + $this->assertSession()->pageTextContains('tag'); + $this->assertSession()->pageTextContains($term->label()); }