From 07307bb7dc2566d905a56f98b5ae6a52ed35c104 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Tue, 10 Oct 2017 10:36:37 +1300 Subject: [PATCH 1/5] Module supported standard files --- .editorconfig | 17 ++++++++++++++++ .gitattributes | 6 ++++++ .scrutinizer.yml | 12 +++++++++++ .travis.yml | 37 ++++++++++++++++++++++++++++++++++ README.md | 22 ++++++++++++++++++-- codecov.yml | 1 + composer.json | 17 ++++++++++++---- contributing.md | 25 +++++++++++++++++++++++ phpunit.xml.dist | 14 +++++++++++++ tests/php/ElementFormTests.php | 13 ++++++++++++ 10 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .scrutinizer.yml create mode 100644 .travis.yml create mode 100644 codecov.yml create mode 100644 contributing.md create mode 100644 phpunit.xml.dist create mode 100644 tests/php/ElementFormTests.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..47ae637 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# For more information about the properties used in this file, +# please see the EditorConfig documentation: +# http://editorconfig.org + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[{*.yml,package.json}] +indent_size = 2 + +# The indent size used in the package.json file cannot be changed: +# https://github.com/npm/npm/pull/3180#issuecomment-16336516 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..475f5f2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +/tests export-ignore +/docs export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/.scrutinizer.yml export-ignore diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..3716f81 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,12 @@ +inherit: true + +tools: + external_code_coverage: false + +checks: + php: + code_rating: true + duplication: true + +filter: + paths: [src/*, tests/*] diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2139d06 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,37 @@ +language: php + +dist: trusty + +env: + global: + - COMPOSER_ROOT_VERSION=1.x-dev + +matrix: + include: + - php: 5.6 + env: DB=MYSQL PHPCS_TEST=1 PHPUNIT_TEST=1 + - php: 7.0 + env: DB=PGSQL PHPUNIT_TEST=1 + - php: 7.0 + env: DB=MYSQL PHPUNIT_TEST=1 + - php: 7.1 + env: DB=MYSQL PHPUNIT_COVERAGE_TEST=1 + +before_script: + # Init PHP + - phpenv rehash + - phpenv config-rm xdebug.ini + + # Install composer dependencies + - composer validate + - composer require silverstripe/recipe-cms 1.0.x-dev --no-update + - if [[ $DB == PGSQL ]]; then composer require silverstripe/postgresql:2.0.x-dev --no-update; fi + - composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile + +script: + - if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit tests/; fi + - if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml; fi + - if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs --standard=vendor/silverstripe/framework/phpcs.xml.dist src/ tests/ ; fi + +after_success: + - if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi diff --git a/README.md b/README.md index 482fcda..1a0fcdd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ -# silverstripe-elemental-userforms -Adds a new element for usersforms +# SilverStripe Elemental UserForms + +[![Build Status](http://img.shields.io/travis/ddnadesign/silverstripe-elemental-userforms.svg?style=flat-square)](https://travis-ci.org/dnadesign/silverstripe-elemental) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/dnadesign/silverstripe-elemental-userforms/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/dnadesign/silverstripe-elemental-userforms/?branch=master) +[![codecov](https://codecov.io/gh/dnadesign/silverstripe-elemental-userforms/branch/master/graph/badge.svg)](https://codecov.io/gh/dnadesign/silverstripe-elemental) +[![Version](http://img.shields.io/packagist/v/dnadesign/silverstripe-elemental-userforms.svg?style=flat-square)](https://packagist.org/packages/dnadesign/silverstripe-elemental-userforms) +[![License](http://img.shields.io/packagist/l/dnadesign/silverstripe-elemental-userforms.svg?style=flat-square)](LICENSE.md) + +## Introduction + +Adds a new form element for SilverStripe Elemental. This block allows users to +construct User Forms using the SilverStripe UserForms module. + +## Installation + +``` +composer require "dnadesign/silverstripe-elemental-userforms" +``` + + diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..69cb760 --- /dev/null +++ b/codecov.yml @@ -0,0 +1 @@ +comment: false diff --git a/composer.json b/composer.json index f67cd17..16bd475 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dnadesign/silverstripe-elemental-userforms", "description": "Adds a new element for usersforms", - "type": "silverstripe-module", + "type": "silverstripe-vendormodule", "keywords": ["silverstripe", "element", "elemental", "content blocks", "userforms"], "license": "BSD-3-Clause", "authors": [{ @@ -12,9 +12,18 @@ "prefer-stable": true, "require": { "dnadesign/silverstripe-elemental": "dev-ss4", - "silverstripe/userforms": "dev-master" + "silverstripe/userforms": "5.0.x-dev" }, "extra": { - "installer-name": "elemental-userforms" - } + "installer-name": "elemental-userforms", + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "DNADesign\\Elemental\\UserForms\\": "src/", + "DNADesign\\Elemental\\UserForms\\Tests\\": "tests/php/" + } + } } diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..8ba55ba --- /dev/null +++ b/contributing.md @@ -0,0 +1,25 @@ +# Contributing + +Any open source product is only as good as the community behind it. You can participate by sharing code, ideas, or +simply helping others. No matter what your skill level is, every contribution counts. + +See our [high level overview](http://silverstripe.org/contributing-to-silverstripe) on silverstripe.org on how you can +help out. + +Or, for more detailed guidance, read one of the following pages: + + * [Sharing your opinion and raising issues](http://docs.silverstripe.org/en/contributing/issues_and_bugs/) + * [Providing code, whether it's creating a feature or fixing a bug](http://docs.silverstripe.org/en/contributing/code/) + * [Writing and translating documentation](http://docs.silverstripe.org/en/contributing/translations/) + * [Translating user-interface elements](http://docs.silverstripe.org/en/contributing/translation_process/) + +## Copyright + +**IMPORTANT: By supplying code in patches, tickets and pull requests, you agree to assign copyright of that code to +DNA DESIGNED COMMUNICATIONS LIMITED, on the condition that DNA DESIGNED COMMUNICATIONS LIMITED releases that code under +the BSD license.** unless otherwise noted. + +We ask for this so that the ownership in the license is clear and unambiguous, and so that community involvement doesn't +stop us from being able to continue supporting these projects. By releasing this code under a permissive license, this +copyright assignment won't prevent you from using the code in any way you see fit. + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..49affdc --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,14 @@ + + + tests/php + + + + + src/ + + tests/php + + + + diff --git a/tests/php/ElementFormTests.php b/tests/php/ElementFormTests.php new file mode 100644 index 0000000..4ced6ba --- /dev/null +++ b/tests/php/ElementFormTests.php @@ -0,0 +1,13 @@ + Date: Tue, 10 Oct 2017 10:37:25 +1300 Subject: [PATCH 2/5] =?UTF-8?q?Modify=20ElementForm=20to=20contain=20its?= =?UTF-8?q?=E2=80=99=20own=20form=20builder.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- images/form.svg | 18 ++++++ src/Control/ElementFormController.php | 25 +++++++++ src/Model/ElementForm.php | 55 +++++++++++++++++++ src/Model/ElementUserDefinedForm.php | 38 ------------- .../Elemental/UserForms/Model/ElementForm.ss | 3 + 5 files changed, 101 insertions(+), 38 deletions(-) create mode 100644 images/form.svg create mode 100644 src/Control/ElementFormController.php create mode 100644 src/Model/ElementForm.php delete mode 100644 src/Model/ElementUserDefinedForm.php create mode 100644 templates/DNADesign/Elemental/UserForms/Model/ElementForm.ss diff --git a/images/form.svg b/images/form.svg new file mode 100644 index 0000000..6c23ea3 --- /dev/null +++ b/images/form.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + diff --git a/src/Control/ElementFormController.php b/src/Control/ElementFormController.php new file mode 100644 index 0000000..fab316d --- /dev/null +++ b/src/Control/ElementFormController.php @@ -0,0 +1,25 @@ +getRequest()->isPOST()) { + // handle the post request. + $user = UserDefinedFormController::create($element); + $form = $user->Form(); + + $user->process($current->getRequest()->postVars(), $form); + } + } +} diff --git a/src/Model/ElementForm.php b/src/Model/ElementForm.php new file mode 100644 index 0000000..ad90af0 --- /dev/null +++ b/src/Model/ElementForm.php @@ -0,0 +1,55 @@ +getAction() == 'finished') { + return $controller->renderWith('ReceivedFormSubmission'); + } + + $form = $controller->Form(); + $form->setFormAction(Controller::join_links( + $current->Link(), + 'element', + $this->owner->ID + )); + + return $form; + } + + +} diff --git a/src/Model/ElementUserDefinedForm.php b/src/Model/ElementUserDefinedForm.php deleted file mode 100644 index 2897473..0000000 --- a/src/Model/ElementUserDefinedForm.php +++ /dev/null @@ -1,38 +0,0 @@ - UserDefinedForm::class - ); - - private static $table_name = 'ElementForm'; - - private static $title = 'Form Element'; - - public function ElementForm() - { - if ($this->Form()->exists()) { - $controller = new UserDefinedForm_Controller($this->Form()); - - $current = Controller::curr(); - - if ($current && $current->getAction() == 'finished') { - return $controller->renderWith('ReceivedFormSubmission'); - } - - $form = $controller->Form(); - - return $form; - } - } -} diff --git a/templates/DNADesign/Elemental/UserForms/Model/ElementForm.ss b/templates/DNADesign/Elemental/UserForms/Model/ElementForm.ss new file mode 100644 index 0000000..4c17e96 --- /dev/null +++ b/templates/DNADesign/Elemental/UserForms/Model/ElementForm.ss @@ -0,0 +1,3 @@ +
+ $ElementForm +
From 5a7de6c250d79662fb923df6ab17f80c04df8b14 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Tue, 10 Oct 2017 15:12:48 +1300 Subject: [PATCH 3/5] PHPCS lint --- src/Control/ElementFormController.php | 7 +++++++ src/Model/ElementForm.php | 2 -- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Control/ElementFormController.php b/src/Control/ElementFormController.php index fab316d..113e17f 100644 --- a/src/Control/ElementFormController.php +++ b/src/Control/ElementFormController.php @@ -6,8 +6,15 @@ use SilverStripe\Control\Controller; use SilverStripe\UserForms\Control\UserDefinedFormController; +/** + * Handles Form Submissions + * + */ class ElementFormController extends ElementController { + /** + * {@inheritDoc} + */ public function __construct($element = null) { parent::__construct($element); diff --git a/src/Model/ElementForm.php b/src/Model/ElementForm.php index ad90af0..d261ce8 100644 --- a/src/Model/ElementForm.php +++ b/src/Model/ElementForm.php @@ -50,6 +50,4 @@ public function ElementForm() return $form; } - - } From 1e6d3fa21616a71c900ae149799e57077c75b7e2 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Tue, 10 Oct 2017 15:55:09 +1300 Subject: [PATCH 4/5] Vendorize --- composer.json | 8 ++++++-- src/Model/ElementForm.php | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 16bd475..9b706f8 100644 --- a/composer.json +++ b/composer.json @@ -12,13 +12,17 @@ "prefer-stable": true, "require": { "dnadesign/silverstripe-elemental": "dev-ss4", - "silverstripe/userforms": "5.0.x-dev" + "silverstripe/userforms": "5.0.x-dev", + "silverstripe/vendor-plugin": "^1.0" }, "extra": { "installer-name": "elemental-userforms", "branch-alias": { "dev-master": "1.x-dev" - } + }, + "expose": [ + "images" + ] }, "autoload": { "psr-4": { diff --git a/src/Model/ElementForm.php b/src/Model/ElementForm.php index d261ce8..8515952 100644 --- a/src/Model/ElementForm.php +++ b/src/Model/ElementForm.php @@ -25,7 +25,7 @@ class ElementForm extends BaseElement /** * @var string */ - private static $icon = 'elemental-userforms/images/form.svg'; + private static $icon = 'dnadesign:silverstripe-elemental-userforms:images/form.svg'; /** * @var string From 8115cfe7cb163b4c4712702dc7c4340400e2c008 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 16 Oct 2017 10:19:00 +1300 Subject: [PATCH 5/5] Update namespace --- README.md | 4 +- composer.json | 4 +- src/Control/ElementFormController.php | 25 ++++++- src/Model/ElementForm.php | 21 ++++-- .../Elemental/UserForms/Model/ElementForm.ss | 3 - .../ElementalUserForms/Model/ElementForm.ss | 3 + tests/php/ElementFormControllerTest.php | 73 +++++++++++++++++++ ...ementFormTests.php => ElementFormTest.php} | 2 +- tests/php/ElementFormTest.yml | 14 ++++ 9 files changed, 131 insertions(+), 18 deletions(-) delete mode 100644 templates/DNADesign/Elemental/UserForms/Model/ElementForm.ss create mode 100644 templates/DNADesign/ElementalUserForms/Model/ElementForm.ss create mode 100644 tests/php/ElementFormControllerTest.php rename tests/php/{ElementFormTests.php => ElementFormTest.php} (77%) create mode 100644 tests/php/ElementFormTest.yml diff --git a/README.md b/README.md index 1a0fcdd..d1a60f8 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # SilverStripe Elemental UserForms -[![Build Status](http://img.shields.io/travis/ddnadesign/silverstripe-elemental-userforms.svg?style=flat-square)](https://travis-ci.org/dnadesign/silverstripe-elemental) +[![Build Status](http://img.shields.io/travis/dnadesign/silverstripe-elemental-userforms.svg?style=flat-square)](https://travis-ci.org/dnadesign/silverstripe-elemental-userforms) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/dnadesign/silverstripe-elemental-userforms/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/dnadesign/silverstripe-elemental-userforms/?branch=master) -[![codecov](https://codecov.io/gh/dnadesign/silverstripe-elemental-userforms/branch/master/graph/badge.svg)](https://codecov.io/gh/dnadesign/silverstripe-elemental) +[![codecov](https://codecov.io/gh/dnadesign/silverstripe-elemental-userforms/branch/master/graph/badge.svg)](https://codecov.io/gh/dnadesign/silverstripe-elemental-userforms) [![Version](http://img.shields.io/packagist/v/dnadesign/silverstripe-elemental-userforms.svg?style=flat-square)](https://packagist.org/packages/dnadesign/silverstripe-elemental-userforms) [![License](http://img.shields.io/packagist/l/dnadesign/silverstripe-elemental-userforms.svg?style=flat-square)](LICENSE.md) diff --git a/composer.json b/composer.json index 9b706f8..9b4d29a 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,8 @@ }, "autoload": { "psr-4": { - "DNADesign\\Elemental\\UserForms\\": "src/", - "DNADesign\\Elemental\\UserForms\\Tests\\": "tests/php/" + "DNADesign\\ElementalUserForms\\": "src/", + "DNADesign\\ElementalUserForms\\Tests\\": "tests/php/" } } } diff --git a/src/Control/ElementFormController.php b/src/Control/ElementFormController.php index 113e17f..5311322 100644 --- a/src/Control/ElementFormController.php +++ b/src/Control/ElementFormController.php @@ -1,6 +1,6 @@ process($current->getRequest()->postVars(), $form); } } + + /** + * @param string $action + * + * @return string + */ + public function Link($action = null) + { + $id = $this->element->ID; + $segment = Controller::join_links('element', $id, $action); + $page = Director::get_current_page(); + + if ($page && !($page instanceof ElementController)) { + return $page->Link($segment); + } + + if ($controller = $this->getParentController()) { + return $controller->Link($segment); + } + + return $segment; + } } diff --git a/src/Model/ElementForm.php b/src/Model/ElementForm.php index 8515952..d445f0e 100644 --- a/src/Model/ElementForm.php +++ b/src/Model/ElementForm.php @@ -1,12 +1,12 @@ Form(); - $form->setFormAction(Controller::join_links( - $current->Link(), - 'element', - $this->owner->ID - )); + $form->setFormAction( + Controller::join_links( + $current->Link(), + 'element', + $this->owner->ID + ) + ); return $form; } diff --git a/templates/DNADesign/Elemental/UserForms/Model/ElementForm.ss b/templates/DNADesign/Elemental/UserForms/Model/ElementForm.ss deleted file mode 100644 index 4c17e96..0000000 --- a/templates/DNADesign/Elemental/UserForms/Model/ElementForm.ss +++ /dev/null @@ -1,3 +0,0 @@ -
- $ElementForm -
diff --git a/templates/DNADesign/ElementalUserForms/Model/ElementForm.ss b/templates/DNADesign/ElementalUserForms/Model/ElementForm.ss new file mode 100644 index 0000000..5383ec2 --- /dev/null +++ b/templates/DNADesign/ElementalUserForms/Model/ElementForm.ss @@ -0,0 +1,3 @@ +
+ $ElementForm +
diff --git a/tests/php/ElementFormControllerTest.php b/tests/php/ElementFormControllerTest.php new file mode 100644 index 0000000..919011e --- /dev/null +++ b/tests/php/ElementFormControllerTest.php @@ -0,0 +1,73 @@ +logInWithPermission('ADMIN'); + $page = $this->objFromFixture(TestPage::class, 'page1'); + + $element = $this->objFromFixture(TestElement::class, 'element1'); + + $response = $this->get($page->URLSegment); + $formAction = sprintf('%s/element/%d/Form', $page->URLSegment, $element->ID); + + $this->assertContains( + $formAction, + $response->getBody(), + 'Element forms are rendered through ElementalArea templates' + ); + } + + public function testElementFormSubmission() + { + $this->logInWithPermission('ADMIN'); + $page = $this->objFromFixture(TestPage::class, 'page1'); + + $element = $this->objFromFixture(TestElement::class, 'element1'); + + $response = $this->get($page->URLSegment); + $response = $this->submitForm('Form_Form', null, array('TestValue' => 'Updated')); + + $this->assertContains( + 'TestValue: Updated', + $response->getBody(), + 'Form values are submitted to correct element form' + ); + $this->assertContains( + sprintf('Element ID: %d', $element->ID), + $response->getBody(), + 'Element form acts on correct element, as identified in the URL' + ); + } +} diff --git a/tests/php/ElementFormTests.php b/tests/php/ElementFormTest.php similarity index 77% rename from tests/php/ElementFormTests.php rename to tests/php/ElementFormTest.php index 4ced6ba..0054430 100644 --- a/tests/php/ElementFormTests.php +++ b/tests/php/ElementFormTest.php @@ -1,6 +1,6 @@ DNADesign\Elemental\Models\ElementalArea.area1 + +DNADesign\Elemental\Tests\Src\TestElement: + element1: + Title: Element 1 + ParentID: =>DNADesign\Elemental\Models\ElementalArea.area1