From 9da68826e70b32922e5d581b8648d9c2f176627e Mon Sep 17 00:00:00 2001 From: Andrea R Soper Date: Mon, 1 Aug 2016 13:44:40 -0500 Subject: [PATCH 1/5] Add a Field context to check for the existence of fields. --- .../PalantirBehatExtension/Context/FieldContext.php | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php diff --git a/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php b/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php new file mode 100644 index 0000000..b235ce8 --- /dev/null +++ b/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php @@ -0,0 +1,9 @@ + Date: Mon, 1 Aug 2016 13:56:23 -0500 Subject: [PATCH 2/5] Actually add the things. --- .../Context/FieldContext.php | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php b/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php index b235ce8..af1a6af 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php @@ -7,3 +7,113 @@ */ namespace Palantirnet\PalantirBehatExtension\Context; +use Behat\Gherkin\Node\TableNode; + +class FieldContext extends SharedDrupalContext { + + + /** + * Asserts a page has fields provided in the form of a given type: + * | field | tag | type | + * | title | input | text | + * | body | textarea | | + * | field-subheadline | input | text | + * | field-author | input | text | + * | field-summary | textarea | | + * | field-full-text | textarea | | + * | field-ref-sections | select | | + * + * Assumes fields are targeted with #edit-. For example, + * "body" checks for the existence of the element, "#edit-body". Note, for + * almost everything this will begin with "field-", like "field-tags". + * + * @Then the form at :path has the expected fields: + * @Then the content type :type has the expected fields: + * + * @param String $path + * @param TableNode $fieldsTable + * @param String $content_type + * @throws \Exception + */ + public function assertFields($path = '', $content_type = '', TableNode $fieldsTable) { + // Load the page with the form on it. + if (empty($path)) { + $path = 'node/add/' . $content_type; + } + $this->getSession()->visit($this->locatePath($path)); + $page = $this->getSession()->getPage(); + + foreach ($fieldsTable->getHash() as $row) { + $fieldSelector = '#edit-' . $row['field']; + $page->hasField($fieldSelector); + $this->assertFieldType('#edit-' . $row['field'], $row['tag'], $row['type']); + } + } + + /** + * Test a field on the current page to see if it matches + * the expected HTML field type. + * + * @Then the ":field" field is ":tag" + * @Then the ":field" field is ":tag" with type ":type" + * + * @param string $field + * @param string $expectedTag + * @param string $expectedType + * @throws Exception + */ + public function assertFieldType($field, $expectedTag, $expectedType = '') { + $callback = 'assert' . ucfirst($expectedTag); + if (!method_exists($this, $callback)) { + throw new Exception(sprintf('%s is not a field tag we know how to validate.', + $expectedTag)); + } + $this->$callback($field, $expectedType); + } + + /** + * Verify the field is a textarea. + * + * @param $field + * @param $expectedType + * @throws Exception + */ + public function assertTextarea($field, $expectedType) { + $element = $this->getSession()->getPage()->find('css', $field); + if (NULL == $element->find('css', 'textarea.form-textarea')) { + throw new Exception(sprintf("Couldn't find %s of type textarea.", $field)); + } + } + + /** + * Verify the field is an input field of the given type. + * + * @param $field + * @param $expectedType + * @throws Exception + */ + public function assertInput($field, $expectedType) { + $element = $this->getSession()->getPage()->find('css', $field); + if (NULL == $element || NULL == $element->find('css', 'input[type="' . $expectedType . '"]')) { + throw new Exception(sprintf("Couldn't find %s of type %s", $field, $expectedType)); + } + } + + /** + * Verify the field is a select list. + * + * @param $field + * @param $expectedType + * @throws Exception + */ + public function assertSelect($field, $expectedType) { + $element = $this->getSession()->getPage()->find('css', $field); + if (NULL == $element->find('css', 'select.form-select')) { + throw new Exception(sprintf("Couldn't find %s of type select.", $field)); + } + // Verify that the select list is not part of a multivalue widget. + if (!$element->find('css', 'select.form-select')->isVisible()) { + throw new Exception(sprintf("Couldn't find %s of type select.", $field)); + } + } +} From c9a37e4ea1076048cf4911689b69474f827ff15c Mon Sep 17 00:00:00 2001 From: Andrea R Soper Date: Mon, 1 Aug 2016 14:19:00 -0500 Subject: [PATCH 3/5] Fix some easy linting things. --- .../Context/FieldContext.php | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php b/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php index af1a6af..2a8eb5f 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php @@ -1,7 +1,6 @@ getSession()->getPage()->find('css', $field); - if (NULL == $element->find('css', 'textarea.form-textarea')) { + if (null == $element->find('css', 'textarea.form-textarea')) { throw new Exception(sprintf("Couldn't find %s of type textarea.", $field)); } } @@ -88,13 +93,13 @@ public function assertTextarea($field, $expectedType) { /** * Verify the field is an input field of the given type. * - * @param $field - * @param $expectedType - * @throws Exception + * @param $field + * @param $expectedType + * @throws Exception */ public function assertInput($field, $expectedType) { $element = $this->getSession()->getPage()->find('css', $field); - if (NULL == $element || NULL == $element->find('css', 'input[type="' . $expectedType . '"]')) { + if (null == $element || null == $element->find('css', 'input[type="' . $expectedType . '"]')) { throw new Exception(sprintf("Couldn't find %s of type %s", $field, $expectedType)); } } @@ -102,13 +107,13 @@ public function assertInput($field, $expectedType) { /** * Verify the field is a select list. * - * @param $field - * @param $expectedType - * @throws Exception + * @param $field + * @param $expectedType + * @throws Exception */ public function assertSelect($field, $expectedType) { $element = $this->getSession()->getPage()->find('css', $field); - if (NULL == $element->find('css', 'select.form-select')) { + if (null == $element->find('css', 'select.form-select')) { throw new Exception(sprintf("Couldn't find %s of type select.", $field)); } // Verify that the select list is not part of a multivalue widget. From 4449f559981d61d2cf2d98d5687e706057904db4 Mon Sep 17 00:00:00 2001 From: Andrea R Soper Date: Mon, 1 Aug 2016 17:04:34 -0500 Subject: [PATCH 4/5] Fix some more linting things. --- .../Context/FieldContext.php | 247 ++++++++++-------- 1 file changed, 142 insertions(+), 105 deletions(-) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php b/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php index 2a8eb5f..df4df3a 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/FieldContext.php @@ -1,8 +1,8 @@ . For example, - * "body" checks for the existence of the element, "#edit-body". Note, for - * almost everything this will begin with "field-", like "field-tags". - * - * @Then the form at :path has the expected fields: - * @Then the content type :type has the expected fields: - * - * @param String $path - * @param TableNode $fieldsTable - * @param String $content_type - * - * @throws \Exception - */ - public function assertFields($path = '', $content_type = '', TableNode $fieldsTable) { - // Load the page with the form on it. - if (empty($path)) { - $path = 'node/add/' . $content_type; - } - $this->getSession()->visit($this->locatePath($path)); - $page = $this->getSession()->getPage(); +class FieldContext extends SharedDrupalContext +{ + + + /** + * Asserts a page has fields provided in the form of a given type: + * + * Assumes fields are targeted with #edit-. For example, "body" + * checks for the existence of the element, "#edit-body". Note, for almost + * everything this will begin with "field-", like "field-tags". + * + * @Then the form at :path has the expected fields: + * @Then the content type :type has the expected fields: + * + * @param TableNode $fieldsTable A table of fields to check with field types. + * @param String $path The path to the form to check fields on. + * @param String $content_type Optional content type to derive the form path from. + * | field | tag | type | + * | title | input | text | + * | body | textarea | | + * | field-subheadline | input | text | + * | field-author | input | text | + * | field-summary | textarea | | + * | field-full-text | textarea | | + * | field-ref-sections | select | | + * + * @return bool + * @throws \Exception + */ + public function assertFields(TableNode $fieldsTable, $path = '', $content_type = '') + { + // Load the page with the form on it. + if (true === empty($path)) { + $path = 'node/add/'.$content_type; + } + + $this->getSession()->visit($this->locatePath($path)); + $page = $this->getSession()->getPage(); + + foreach ($fieldsTable->getHash() as $row) { + $fieldSelector = '#edit-'.$row['field']; + $page->hasField($fieldSelector); + $this->assertFieldType('#edit-'.$row['field'], $row['tag'], $row['type']); + } + + return true; - foreach ($fieldsTable->getHash() as $row) { - $fieldSelector = '#edit-' . $row['field']; - $page->hasField($fieldSelector); - $this->assertFieldType('#edit-' . $row['field'], $row['tag'], $row['type']); - } - } - - /** - * Test a field on the current page to see if it matches - * the expected HTML field type. - * - * @Then the ":field" field is ":tag" - * @Then the ":field" field is ":tag" with type ":type" - * - * @param string $field - * @param string $expectedTag - * @param string $expectedType - * @throws Exception - */ - public function assertFieldType($field, $expectedTag, $expectedType = '') { - $callback = 'assert' . ucfirst($expectedTag); - if (!method_exists($this, $callback)) { - throw new Exception(sprintf('%s is not a field tag we know how to validate.', - $expectedTag)); } - $this->$callback($field, $expectedType); - } - - /** - * Verify the field is a textarea. - * - * @param $field - * @param $expectedType - * @throws Exception - */ - public function assertTextarea($field, $expectedType) { - $element = $this->getSession()->getPage()->find('css', $field); - if (null == $element->find('css', 'textarea.form-textarea')) { - throw new Exception(sprintf("Couldn't find %s of type textarea.", $field)); + + + /** + * Test a field on the page to see if it matches the expected HTML field type. + * + * @Then the ":field" field is ":tag" + * @Then the ":field" field is ":tag" with type ":type" + * + * @param string $field Field selector used when testing this field. + * @param string $expectedTag Expected tag for this field. + * @param string $expectedType Expected "type" used with this field. + * + * @return bool + * @throws Exception + */ + public function assertFieldType($field, $expectedTag, $expectedType = '') + { + $callback = 'assert'.ucfirst($expectedTag); + if (false === method_exists($this, $callback)) { + throw new Exception(sprintf('%s is not a field tag we know how to validate.', $expectedTag)); + } + + $this->$callback($field, $expectedType); + + return true; + } - } - - /** - * Verify the field is an input field of the given type. - * - * @param $field - * @param $expectedType - * @throws Exception - */ - public function assertInput($field, $expectedType) { - $element = $this->getSession()->getPage()->find('css', $field); - if (null == $element || null == $element->find('css', 'input[type="' . $expectedType . '"]')) { - throw new Exception(sprintf("Couldn't find %s of type %s", $field, $expectedType)); + + + /** + * Verify the field is a textarea. + * + * @param string $field Field selector used when testing this field. + * @param string $expectedType Expected "type" used with this field. + * + * @return bool + * @throws Exception + */ + public function assertTextarea($field, $expectedType = '') + { + $element = $this->getSession()->getPage()->find('css', $field); + if (null === $element->find('css', 'textarea.form-textarea')) { + throw new Exception(sprintf("Couldn't find %s of type textarea.", $field)); + } + + return true; + } - } - - /** - * Verify the field is a select list. - * - * @param $field - * @param $expectedType - * @throws Exception - */ - public function assertSelect($field, $expectedType) { - $element = $this->getSession()->getPage()->find('css', $field); - if (null == $element->find('css', 'select.form-select')) { - throw new Exception(sprintf("Couldn't find %s of type select.", $field)); + + + /** + * Verify the field is an input field of the given type. + * + * @param string $field Field selector used when testing this field. + * @param string $expectedType Expected "type" used with this field. + * + * @return bool + * @throws Exception + */ + public function assertInput($field, $expectedType = '') + { + $element = $this->getSession()->getPage()->find('css', $field); + if (null === $element || null === $element->find('css', 'input[type="'.$expectedType.'"]')) { + throw new Exception(sprintf("Couldn't find %s of type %s", $field, $expectedType)); + } + + return true; + } - // Verify that the select list is not part of a multivalue widget. - if (!$element->find('css', 'select.form-select')->isVisible()) { - throw new Exception(sprintf("Couldn't find %s of type select.", $field)); + + + /** + * Verify the field is a select list. + * + * @param string $field Field selector used when testing this field. + * @param string $expectedType Expected "type" used with this field. + * + * @return bool + * @throws Exception + */ + public function assertSelect($field, $expectedType = '') + { + $element = $this->getSession()->getPage()->find('css', $field); + if (null === $element->find('css', 'select.form-select')) { + throw new Exception(sprintf("Couldn't find %s of type select.", $field)); + } + + // Verify that the select list is not part of a multivalue widget. + if (false === $element->find('css', 'select.form-select')->isVisible()) { + throw new Exception(sprintf("Couldn't find %s of type select.", $field)); + } + + return true; + } - } + + } From 5512ae1936161f3a18869ff013c151dcde39219e Mon Sep 17 00:00:00 2001 From: Andrea R Soper Date: Mon, 1 Aug 2016 18:29:03 -0500 Subject: [PATCH 5/5] Don't require //end functionName() at the end of functions/methods. a) If your function/method is so long you can't see the end of it, it is probably too long. b) if you use a proper IDE, it highlights these things for you. --- phpcs.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpcs.xml b/phpcs.xml index fa281b1..af02220 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -13,6 +13,9 @@ before the method parameter documentation. --> + + +