diff --git a/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php b/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php index 24a9bdb58b..98ddc7cb8b 100644 --- a/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php +++ b/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php @@ -64,6 +64,18 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => 2, ]; + $form['duplicate_button'] = [ + '#type' => 'submit', + '#name' => 'duplicate_button', + '#value' => 'Duplicate button 1', + ]; + + $form['duplicate_button_2'] = [ + '#type' => 'submit', + '#name' => 'duplicate_button', + '#value' => 'Duplicate button 2', + ]; + $form['save'] = [ '#type' => 'submit', '#value' => $this->t('Save'), diff --git a/core/modules/user/src/Tests/UserAdminLanguageTest.php b/core/modules/user/tests/src/Functional/UserAdminLanguageTest.php similarity index 86% rename from core/modules/user/src/Tests/UserAdminLanguageTest.php rename to core/modules/user/tests/src/Functional/UserAdminLanguageTest.php index cd6cc657bf..414f159770 100644 --- a/core/modules/user/src/Tests/UserAdminLanguageTest.php +++ b/core/modules/user/tests/src/Functional/UserAdminLanguageTest.php @@ -1,16 +1,16 @@ adminUser->id() . '/edit'; $this->drupalGet($path); // Ensure administration pages language settings widget is not available. - $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available.'); + $this->assertNull($this->getSession()->getPage()->findField('edit-preferred-admin-langcode')); } /** @@ -64,13 +64,13 @@ public function testUserAdminLanguageConfigurationAvailableWithAdminLanguageNego // Checks with user administration pages language negotiation disabled. $this->drupalGet($path); // Ensure administration pages language settings widget is not available. - $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available.'); + $this->assertNull($this->getSession()->getPage()->findField('edit-preferred-admin-langcode')); // Checks with user administration pages language negotiation enabled. $this->setLanguageNegotiation(); $this->drupalGet($path); // Ensure administration pages language settings widget is available. - $this->assertFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector is available.'); + $this->assertNotNull($this->getSession()->getPage()->findField('edit-preferred-admin-langcode')); } /** @@ -91,13 +91,13 @@ public function testUserAdminLanguageConfigurationAvailableIfAdminLanguageNegoti $path = 'user/' . $this->adminUser->id() . '/edit'; $this->drupalGet($path); // Ensure administration pages language setting is visible for admin. - $this->assertFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector available for admins.'); + $this->assertNotNull($this->getSession()->getPage()->findField( 'edit-preferred-admin-langcode')); // Ensure administration pages language setting is hidden for non-admins. $this->drupalLogin($this->regularUser); $path = 'user/' . $this->regularUser->id() . '/edit'; $this->drupalGet($path); - $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available for regular user.'); + $this->assertNull($this->getSession()->getPage()->findField('edit-preferred-admin-langcode')); } /** diff --git a/core/modules/user/src/Tests/UserBlocksTest.php b/core/modules/user/tests/src/Functional/UserBlocksTest.php similarity index 71% rename from core/modules/user/src/Tests/UserBlocksTest.php rename to core/modules/user/tests/src/Functional/UserBlocksTest.php index 0a4c619da6..6c2ae97609 100644 --- a/core/modules/user/src/Tests/UserBlocksTest.php +++ b/core/modules/user/tests/src/Functional/UserBlocksTest.php @@ -1,16 +1,16 @@ getUsername(); - $edit['pass'] = $user->pass_raw; + $edit['pass'] = $user->passRaw; $this->drupalPostForm('admin/people/permissions', $edit, t('Log in')); $this->assertNoText(t('User login'), 'Logged in.'); @@ -121,46 +121,4 @@ public function testUserLoginBlock() { $this->assertNoText(t('Unrecognized username or password. Forgot your password?')); } - /** - * Test the Who's Online block. - */ - public function testWhosOnlineBlock() { - $block = $this->drupalPlaceBlock('views_block:who_s_online-who_s_online_block'); - - // Generate users. - $user1 = $this->drupalCreateUser(['access user profiles']); - $user2 = $this->drupalCreateUser([]); - $user3 = $this->drupalCreateUser([]); - - // Update access of two users to be within the active timespan. - $this->updateAccess($user1->id()); - $this->updateAccess($user2->id(), REQUEST_TIME + 1); - - // Insert an inactive user who should not be seen in the block, and ensure - // that the admin user used in setUp() does not appear. - $inactive_time = REQUEST_TIME - (15 * 60) - 1; - $this->updateAccess($user3->id(), $inactive_time); - $this->updateAccess($this->adminUser->id(), $inactive_time); - - // Test block output. - \Drupal::currentUser()->setAccount($user1); - $content = entity_view($block, 'block'); - $this->setRawContent(\Drupal::service('renderer')->renderRoot($content)); - $this->assertRaw(t('2 users'), 'Correct number of online users (2 users).'); - $this->assertText($user1->getUsername(), 'Active user 1 found in online list.'); - $this->assertText($user2->getUsername(), 'Active user 2 found in online list.'); - $this->assertNoText($user3->getUsername(), 'Inactive user not found in online list.'); - $this->assertTrue(strpos($this->getRawContent(), $user1->getUsername()) > strpos($this->getRawContent(), $user2->getUsername()), 'Online users are ordered correctly.'); - } - - /** - * Updates the access column for a user. - */ - private function updateAccess($uid, $access = REQUEST_TIME) { - db_update('users_field_data') - ->condition('uid', $uid) - ->fields(['access' => $access]) - ->execute(); - } - } diff --git a/core/modules/user/src/Tests/UserCreateTest.php b/core/modules/user/tests/src/Functional/UserCreateTest.php similarity index 94% rename from core/modules/user/src/Tests/UserCreateTest.php rename to core/modules/user/tests/src/Functional/UserCreateTest.php index 7b534435a0..fb7e996e3f 100644 --- a/core/modules/user/src/Tests/UserCreateTest.php +++ b/core/modules/user/tests/src/Functional/UserCreateTest.php @@ -1,18 +1,22 @@ drupalGet('admin/people'); $this->assertText($edit['name'], 'User found in list of users'); $user = user_load_by_name($name); - $this->assertEqual($user->isActive(), 'User is not blocked'); + $this->assertTrue($user->isActive(), 'User is not blocked'); } // Test that the password '0' is considered a password. diff --git a/core/modules/user/src/Tests/UserPasswordResetTest.php b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php similarity index 97% rename from core/modules/user/src/Tests/UserPasswordResetTest.php rename to core/modules/user/tests/src/Functional/UserPasswordResetTest.php index 528d38bc96..fb25317075 100644 --- a/core/modules/user/src/Tests/UserPasswordResetTest.php +++ b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php @@ -1,11 +1,13 @@ drupalLogin($account); $this->account = User::load($account->id()); - $this->account->pass_raw = $account->pass_raw; + $this->account->passRaw = $account->passRaw; $this->drupalLogout(); // Set the last login time that is used to generate the one-time link so @@ -120,7 +129,7 @@ public function testUserPasswordReset() { $edit = [ 'files[user_picture_0]' => drupal_realpath($image->uri), ]; - $this->drupalPostAjaxForm(NULL, $edit, 'user_picture_0_upload_button'); + $this->drupalPostForm(NULL, $edit, 'Upload'); // Change the forgotten password. $password = user_password(); diff --git a/core/modules/user/src/Tests/UserRegistrationTest.php b/core/modules/user/tests/src/Functional/UserRegistrationTest.php similarity index 72% rename from core/modules/user/src/Tests/UserRegistrationTest.php rename to core/modules/user/tests/src/Functional/UserRegistrationTest.php index ca7a4f0c89..5f8b00bf65 100644 --- a/core/modules/user/src/Tests/UserRegistrationTest.php +++ b/core/modules/user/tests/src/Functional/UserRegistrationTest.php @@ -1,20 +1,19 @@ assertEqual($new_user->getUsername(), $name, 'Username matches.'); $this->assertEqual($new_user->getEmail(), $mail, 'Email address matches.'); - $this->assertTrue(($new_user->getCreatedTime() > REQUEST_TIME - 20 ), 'Correct creation time.'); + $this->assertTrue(($new_user->getCreatedTime() > REQUEST_TIME - 20), 'Correct creation time.'); $this->assertEqual($new_user->isActive(), $config_user_settings->get('register') == USER_REGISTER_VISITORS ? 1 : 0, 'Correct status field.'); $this->assertEqual($new_user->getTimezone(), $config_system_date->get('timezone.default'), 'Correct time zone field.'); $this->assertEqual($new_user->langcode->value, \Drupal::languageManager()->getDefaultLanguage()->getId(), 'Correct language field.'); @@ -281,105 +280,6 @@ public function testUniqueFields() { } /** - * Tests Field API fields on user registration forms. - */ - public function testRegistrationWithUserFields() { - // Create a field on 'user' entity type. - $field_storage = FieldStorageConfig::create([ - 'field_name' => 'test_user_field', - 'entity_type' => 'user', - 'type' => 'test_field', - 'cardinality' => 1, - ]); - $field_storage->save(); - $field = FieldConfig::create([ - 'field_storage' => $field_storage, - 'label' => 'Some user field', - 'bundle' => 'user', - 'required' => TRUE, - ]); - $field->save(); - entity_get_form_display('user', 'user', 'default') - ->setComponent('test_user_field', ['type' => 'test_field_widget']) - ->save(); - entity_get_form_display('user', 'user', 'register') - ->save(); - - // Check that the field does not appear on the registration form. - $this->drupalGet('user/register'); - $this->assertNoText($field->label(), 'The field does not appear on user registration form'); - $this->assertCacheTag('config:core.entity_form_display.user.user.register'); - $this->assertCacheTag('config:user.settings'); - - // Have the field appear on the registration form. - entity_get_form_display('user', 'user', 'register') - ->setComponent('test_user_field', ['type' => 'test_field_widget']) - ->save(); - - $this->drupalGet('user/register'); - $this->assertText($field->label(), 'The field appears on user registration form'); - $this->assertRegistrationFormCacheTagsWithUserFields(); - - // Check that validation errors are correctly reported. - $edit = []; - $edit['name'] = $name = $this->randomMachineName(); - $edit['mail'] = $mail = $edit['name'] . '@example.com'; - // Missing input in required field. - $edit['test_user_field[0][value]'] = ''; - $this->drupalPostForm(NULL, $edit, t('Create new account')); - $this->assertRegistrationFormCacheTagsWithUserFields(); - $this->assertRaw(t('@name field is required.', ['@name' => $field->label()]), 'Field validation error was correctly reported.'); - // Invalid input. - $edit['test_user_field[0][value]'] = '-1'; - $this->drupalPostForm(NULL, $edit, t('Create new account')); - $this->assertRegistrationFormCacheTagsWithUserFields(); - $this->assertRaw(t('%name does not accept the value -1.', ['%name' => $field->label()]), 'Field validation error was correctly reported.'); - - // Submit with valid data. - $value = rand(1, 255); - $edit['test_user_field[0][value]'] = $value; - $this->drupalPostForm(NULL, $edit, t('Create new account')); - // Check user fields. - $accounts = $this->container->get('entity_type.manager')->getStorage('user') - ->loadByProperties(['name' => $name, 'mail' => $mail]); - $new_user = reset($accounts); - $this->assertEqual($new_user->test_user_field->value, $value, 'The field value was correctly saved.'); - - // Check that the 'add more' button works. - $field_storage->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); - $field_storage->save(); - foreach (['js', 'nojs'] as $js) { - $this->drupalGet('user/register'); - $this->assertRegistrationFormCacheTagsWithUserFields(); - // Add two inputs. - $value = rand(1, 255); - $edit = []; - $edit['test_user_field[0][value]'] = $value; - if ($js == 'js') { - $this->drupalPostAjaxForm(NULL, $edit, 'test_user_field_add_more'); - $this->drupalPostAjaxForm(NULL, $edit, 'test_user_field_add_more'); - } - else { - $this->drupalPostForm(NULL, $edit, t('Add another item')); - $this->drupalPostForm(NULL, $edit, t('Add another item')); - } - // Submit with three values. - $edit['test_user_field[1][value]'] = $value + 1; - $edit['test_user_field[2][value]'] = $value + 2; - $edit['name'] = $name = $this->randomMachineName(); - $edit['mail'] = $mail = $edit['name'] . '@example.com'; - $this->drupalPostForm(NULL, $edit, t('Create new account')); - // Check user fields. - $accounts = $this->container->get('entity_type.manager')->getStorage('user') - ->loadByProperties(['name' => $name, 'mail' => $mail]); - $new_user = reset($accounts); - $this->assertEqual($new_user->test_user_field[0]->value, $value, format_string('@js : The field value was correctly saved.', ['@js' => $js])); - $this->assertEqual($new_user->test_user_field[1]->value, $value + 1, format_string('@js : The field value was correctly saved.', ['@js' => $js])); - $this->assertEqual($new_user->test_user_field[2]->value, $value + 2, format_string('@js : The field value was correctly saved.', ['@js' => $js])); - } - } - - /** * Asserts the presence of cache tags on registration form with user fields. */ protected function assertRegistrationFormCacheTagsWithUserFields() { diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index 08a2d8ea19..f768b71ad5 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -2,7 +2,6 @@ namespace Drupal\FunctionalTests; -use Behat\Mink\Exception\ElementNotFoundException; use Behat\Mink\Exception\ExpectationException; use Behat\Mink\Selector\Xpath\Escaper; use Drupal\Component\Render\FormattableMarkup; @@ -220,13 +219,11 @@ protected function assertResponse($code) { * * @deprecated Scheduled for removal in Drupal 9.0.0. * Use $this->assertSession()->fieldExists() or + * $this->assertSession()->buttonExists() or * $this->assertSession()->fieldValueEquals() instead. */ protected function assertFieldByName($name, $value = NULL) { - $this->assertSession()->fieldExists($name); - if ($value !== NULL) { - $this->assertSession()->fieldValueEquals($name, (string) $value); - } + $this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value); } /** @@ -242,15 +239,11 @@ protected function assertFieldByName($name, $value = NULL) { * * @deprecated Scheduled for removal in Drupal 9.0.0. * Use $this->assertSession()->fieldNotExists() or + * $this->assertSession()->buttonNotExists() or * $this->assertSession()->fieldValueNotEquals() instead. */ protected function assertNoFieldByName($name, $value = '') { - if ($this->getSession()->getPage()->findField($name) && isset($value)) { - $this->assertSession()->fieldValueNotEquals($name, (string) $value); - } - else { - $this->assertSession()->fieldNotExists($name); - } + $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value); } /** @@ -268,19 +261,11 @@ protected function assertNoFieldByName($name, $value = '') { * * @deprecated Scheduled for removal in Drupal 9.0.0. * Use $this->assertSession()->fieldExists() or + * $this->assertSession()->buttonExists() or * $this->assertSession()->fieldValueEquals() instead. */ protected function assertFieldById($id, $value = '') { - $xpath = $this->assertSession()->buildXPathQuery('//textarea[@id=:value]|//input[@id=:value]|//select[@id=:value]', [':value' => $id]); - $field = $this->getSession()->getPage()->find('xpath', $xpath); - - if (empty($field)) { - throw new ElementNotFoundException($this->getSession()->getDriver(), 'form field', 'id', $field); - } - - if ($value !== NULL) { - $this->assertEquals($value, $field->getValue()); - } + $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value); } /** @@ -290,23 +275,25 @@ protected function assertFieldById($id, $value = '') { * Name or ID of field to assert. * * @deprecated Scheduled for removal in Drupal 9.0.0. - * Use $this->assertSession()->fieldExists() instead. + * Use $this->assertSession()->fieldExists() or + * $this->assertSession()->buttonExists() instead. */ protected function assertField($field) { - $this->assertSession()->fieldExists($field); + $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field)); } /** - * Asserts that a field exists with the given name or ID does NOT exist. + * Asserts that a field does NOT exist with the given name or ID. * * @param string $field * Name or ID of field to assert. * * @deprecated Scheduled for removal in Drupal 9.0.0. - * Use $this->assertSession()->fieldNotExists() instead. + * Use $this->assertSession()->fieldNotExists() or + * $this->assertSession()->buttonNotExists() instead. */ protected function assertNoField($field) { - $this->assertSession()->fieldNotExists($field); + $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field)); } /** @@ -427,23 +414,11 @@ protected function assertNoLinkByHref($href) { * * @deprecated Scheduled for removal in Drupal 9.0.0. * Use $this->assertSession()->fieldNotExists() or + * $this->assertSession()->buttonNotExists() or * $this->assertSession()->fieldValueNotEquals() instead. */ protected function assertNoFieldById($id, $value = '') { - $xpath = $this->assertSession()->buildXPathQuery('//textarea[@id=:value]|//input[@id=:value]|//select[@id=:value]', [':value' => $id]); - $field = $this->getSession()->getPage()->find('xpath', $xpath); - - // Return early if the field could not be found as expected. - if ($field === NULL) { - return; - } - - if (!isset($value)) { - throw new ExpectationException(sprintf('Id "%s" appears on this page, but it should not.', $id), $this->getSession()->getDriver()); - } - elseif ($value === $field->getValue()) { - throw new ExpectationException(sprintf('Failed asserting that %s is not equal to %s', $field->getValue(), $value), $this->getSession()->getDriver()); - } + $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value); } /** @@ -585,25 +560,32 @@ protected function assertFieldByXPath($xpath, $value = NULL, $message = '') { * (optional) A message to display with the assertion. Do not translate * messages with t(). * + * @throws \Behat\Mink\Exception\ExpectationException + * * @deprecated Scheduled for removal in Drupal 9.0.0. * Use $this->xpath() instead and assert that the result is empty. */ protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '') { $fields = $this->xpath($xpath); - // If value specified then check array for match. - $found = TRUE; - if (isset($value)) { - $found = FALSE; - if ($fields) { - foreach ($fields as $field) { - if ($field->getAttribute('value') == $value) { - $found = TRUE; - } + if (!empty($fields)) { + if (isset($value)) { + $found = FALSE; + try { + $this->assertFieldsByValue($fields, $value); + $found = TRUE; + } + catch (\Exception $e) { + } + + if ($found) { + throw new ExpectationException(sprintf('The field resulting from %s was found with the provided value %s.', $xpath, $value), $this->getSession()->getDriver()); } } + else { + throw new ExpectationException(sprintf('The field resulting from %s was found.', $xpath), $this->getSession()->getDriver()); + } } - return $this->assertFalse($fields && $found, $message); } /** @@ -629,7 +611,15 @@ protected function assertFieldsByValue($fields, $value = NULL, $message = '') { $found = FALSE; if ($fields) { foreach ($fields as $field) { - if ($field->getAttribute('value') == $value) { + if ($field->getAttribute('type') == 'checkbox') { + if (is_bool($value)) { + $found = $field->isChecked() == $value; + } + else { + $found = TRUE; + } + } + elseif ($field->getAttribute('value') == $value) { // Input element with correct value. $found = TRUE; } @@ -637,7 +627,7 @@ protected function assertFieldsByValue($fields, $value = NULL, $message = '') { // Select element with an option. $found = TRUE; } - elseif ($field->getText() == $value) { + elseif ($field->getTagName() !== 'input' && $field->getText() == $value) { // Text area with correct text. $found = TRUE; } @@ -789,6 +779,25 @@ protected function buildXPathQuery($xpath, array $args = []) { } /** + * Helper: Constructs an XPath for the given set of attributes and value. + * + * @param string $attribute + * Field attributes. + * @param string $value + * Value of field. + * + * @return string + * XPath for specified values. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->getSession()->getPage()->findField() instead. + */ + protected function constructFieldXpath($attribute, $value) { + $xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]'; + return $this->buildXPathQuery($xpath, [':value' => $value]); + } + + /** * Gets the current raw content. * * @deprecated Scheduled for removal in Drupal 9.0.0. diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index 2701dcb0bf..2195dc9097 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -223,7 +223,7 @@ public function testLegacyXpathAsserts() { $this->assertNoFieldByXPath("//input[@id = 'edit-name']"); $this->fail('The "edit-name" field was not found.'); } - catch (\PHPUnit_Framework_ExpectationFailedException $e) { + catch (ExpectationException $e) { $this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.'); } @@ -239,7 +239,7 @@ public function testLegacyXpathAsserts() { /** * Tests legacy field asserts using textfields. */ - public function testLegacyFieldAssertsWithTextfields() { + public function testLegacyFieldAssertsForTextfields() { $this->drupalGet('test-field-xpath'); // *** 1. assertNoField(). @@ -272,7 +272,7 @@ public function testLegacyFieldAssertsWithTextfields() { $this->assertField('invalid_name_and_id'); $this->fail('The "invalid_name_and_id" field was found.'); } - catch (ExpectationException $e) { + catch (\PHPUnit_Framework_ExpectationFailedException $e) { $this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.'); } @@ -361,7 +361,7 @@ public function testLegacyFieldAssertsWithTextfields() { $this->assertFieldByName('non-existing-name'); $this->fail('The "non-existing-name" field was found.'); } - catch (ExpectationException $e) { + catch (\PHPUnit_Framework_ExpectationFailedException $e) { $this->pass('The "non-existing-name" field was not found'); } @@ -370,15 +370,15 @@ public function testLegacyFieldAssertsWithTextfields() { $this->assertFieldByName('name', 'not the value'); $this->fail('The "name" field with incorrect value was found.'); } - catch (ExpectationException $e) { + catch (\PHPUnit_Framework_ExpectationFailedException $e) { $this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.'); } } /** - * Tests legacy field asserts on other types of field. + * Tests legacy field asserts for options field type. */ - public function testLegacyFieldAssertsWithNonTextfields() { + public function testLegacyFieldAssertsForOptions() { $this->drupalGet('test-field-xpath'); // Option field type. @@ -425,8 +425,14 @@ public function testLegacyFieldAssertsWithNonTextfields() { catch (\PHPUnit_Framework_ExpectationFailedException $e) { $this->pass($e->getMessage()); } + } + + /** + * Tests legacy field asserts for button field type. + */ + public function testLegacyFieldAssertsForButton() { + $this->drupalGet('test-field-xpath'); - // Button field type. $this->assertFieldById('edit-save', NULL); // Test that the assertion fails correctly if the field value is passed in // rather than the id. @@ -434,7 +440,7 @@ public function testLegacyFieldAssertsWithNonTextfields() { $this->assertFieldById('Save', NULL); $this->fail('The field with id of "Save" was found.'); } - catch (ExpectationException $e) { + catch (\PHPUnit_Framework_ExpectationFailedException $e) { $this->pass($e->getMessage()); } @@ -449,7 +455,27 @@ public function testLegacyFieldAssertsWithNonTextfields() { $this->pass($e->getMessage()); } - // Checkbox field type. + // Test that multiple fields with the same name are validated correctly. + $this->assertFieldByName('duplicate_button', 'Duplicate button 1'); + $this->assertFieldByName('duplicate_button', 'Duplicate button 2'); + $this->assertNoFieldByName('duplicate_button', 'Rabbit'); + + try { + $this->assertNoFieldByName('duplicate_button', 'Duplicate button 2'); + $this->fail('The "duplicate_button" field with the value Duplicate button 2 was not found.'); + } + catch (ExpectationException $e) { + $this->pass('assertNoFieldByName correctly failed. The "duplicate_button" field with the value Duplicate button 2 was found.'); + } + } + + /** + * Tests legacy field asserts for checkbox field type. + */ + public function testLegacyFieldAssertsForCheckbox() { + $this->drupalGet('test-field-xpath'); + + // Part 1 - Test by name. // Test that checkboxes are found/not found correctly by name, when using // TRUE or FALSE to match their 'checked' state. $this->assertFieldByName('checkbox_enabled', TRUE); @@ -462,6 +488,24 @@ public function testLegacyFieldAssertsWithNonTextfields() { $this->assertFieldByName('checkbox_enabled', NULL); $this->assertFieldByName('checkbox_disabled', NULL); + // Test that checkboxes are found by name when passing no second parameter. + $this->assertFieldByName('checkbox_enabled'); + $this->assertFieldByName('checkbox_disabled'); + + // Test that we have legacy support. + $this->assertFieldByName('checkbox_enabled', '1'); + $this->assertFieldByName('checkbox_disabled', ''); + + // Test that the assertion fails correctly when using NULL to ignore state. + try { + $this->assertNoFieldByName('checkbox_enabled', NULL); + $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.'); + } + catch (ExpectationException $e) { + $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.'); + } + + // Part 2 - Test by ID. // Test that checkboxes are found/not found correctly by ID, when using // TRUE or FALSE to match their 'checked' state. $this->assertFieldById('edit-checkbox-enabled', TRUE); @@ -469,19 +513,18 @@ public function testLegacyFieldAssertsWithNonTextfields() { $this->assertNoFieldById('edit-checkbox-enabled', FALSE); $this->assertNoFieldById('edit-checkbox-disabled', TRUE); - // Test that checkboxes are found by by ID, when using NULL to ignore the + // Test that checkboxes are found by ID, when using NULL to ignore the // 'checked' state. $this->assertFieldById('edit-checkbox-enabled', NULL); $this->assertFieldById('edit-checkbox-disabled', NULL); - // Test that the assertion fails correctly when using NULL to ignore state. - try { - $this->assertNoFieldByName('checkbox_enabled', NULL); - $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.'); - } - catch (ExpectationException $e) { - $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.'); - } + // Test that checkboxes are found by ID when passing no second parameter. + $this->assertFieldById('edit-checkbox-enabled'); + $this->assertFieldById('edit-checkbox-disabled'); + + // Test that we have legacy support. + $this->assertFieldById('edit-checkbox-enabled', '1'); + $this->assertFieldById('edit-checkbox-disabled', ''); // Test that the assertion fails correctly when using NULL to ignore state. try { @@ -492,7 +535,7 @@ public function testLegacyFieldAssertsWithNonTextfields() { $this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.'); } - // Test the specific 'checked' assertions. + // Part 3 - Test the specific 'checked' assertions. $this->assertFieldChecked('edit-checkbox-enabled'); $this->assertNoFieldChecked('edit-checkbox-disabled');