diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 896edad..43445d1 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -12,11 +12,15 @@ updates: directory: "/" schedule: interval: "daily" + commit-message: + prefix: Bump assignees: - 'mleutenegger' - package-ecosystem: github-actions directory: "/" schedule: interval: "daily" + commit-message: + prefix: Bump assignees: - 'mleutenegger' diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 03a0327..d29f886 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,84 +7,10 @@ env: SS_ENVIRONMENT_TYPE: "dev" RECIPE_CMS_VERSION: 4.x-dev jobs: - phpunit: - name: 🧩 PHPUnit - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - php: - - 7.4 - container: brettt89/silverstripe-web:${{ matrix.php }}-apache - services: - database: - image: mysql:5.7 - ports: - - 3306:3306 - options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 - env: - MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' - env: - SS_DEFAULT_ADMIN_USERNAME: admin - SS_DEFAULT_ADMIN_PASSWORD: admin - SS_DATABASE_SERVER: database - SS_DATABASE_NAME: ss_default_${{ matrix.php }} - SS_DATABASE_USERNAME: root - SS_DATABASE_PASSWORD: '' - SS_ENVIRONMENT_TYPE: dev - steps: - - name: Install Composer - run: | - curl \ - -sS https://getcomposer.org/installer \ - | php && \ - mv -f composer.phar /usr/local/bin/composer - - name: Checkout code - uses: actions/checkout@v3.0.2 - - name: install dependencies - run: | - composer require --no-update silverstripe/recipe-cms:$RECIPE_CMS_VERSION &&\ - composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile - - name: run phpunit - run: vendor/bin/phpunit -c "phpunit.xml" tests/ - - name: generate coverage - run: phpdbg -qrr vendor/bin/phpunit -dmemory_limit=512M --coverage-clover=coverage.xml tests/ - if: ${{ matrix.php == '7.4' }} - - name: submit coverage - uses: codecov/codecov-action@v3.1.0 - with: - file: ./coverage.xml - if: ${{ matrix.php == '7.4' }} - phpstan: - name: 🔺 PHPStan - runs-on: ubuntu-latest - container: brettt89/silverstripe-web:7.4-apache - steps: - - name: Install Composer - run: | - curl \ - -sS https://getcomposer.org/installer \ - | php && \ - mv -f composer.phar /usr/local/bin/composer - - name: Checkout code - uses: actions/checkout@v3.0.2 - - name: install dependencies - run: | - composer require --no-update silverstripe/recipe-cms:$RECIPE_CMS_VERSION &&\ - composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile - - name: run phpstan - run: vendor/bin/phpstan analyse src/ -c "phpstan.neon" -a vendor/syntro/silverstripe-phpstan/bootstrap.php --level 4 - phpcs: - name: 🔮 PHPcs - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v3.0.2 - - name: lint source - uses: chindit/actions-phpcs@master - with: - dir: src/ - - name: lint tests - uses: chindit/actions-phpcs@master - with: - dir: tests/ + silverstripe-module: + name: 🧰 Silverstripe Module Testsuite + uses: syntro-opensource/workflows/.github/workflows/silverstripe-module.yml@master + with: + phpunit: true + phpstan: true + phpcs: true diff --git a/src/Dev/FormPage.php b/src/Dev/FormPage.php new file mode 100644 index 0000000..9308524 --- /dev/null +++ b/src/Dev/FormPage.php @@ -0,0 +1,15 @@ + true + ]; + + /** + * Form + * + * @return Form + */ + public function Form() + { + $fields = new FieldList( + $checkboxfield = CheckboxField::create('checkboxfield', 'checkboxfield'), + $checkboxsetfield = CheckboxSetField::create('checkboxsetfield', 'checkboxsetfield', ['a' => 'value a', 'b' => 'value b']), + $dropdownfield = DropdownField::create('dropdownfield', 'dropdownfield', ['a' => 'value a', 'b' => 'value b']), + $emailfield = EmailField::create('emailfield', 'emailfield'), + $optionsetfield = OptionsetField::create('optionsetfield', 'optionsetfield', ['a' => 'value a', 'b' => 'value b']), + $phonefield = PhoneField::create('phonefield', 'phonefield'), + $textareafield = TextareaField::create('textareafield', 'textareafield'), + $textfield = TextField::create('textfield', 'textfield'), + ); + + $checkboxfield->addHolderClass('checkboxfieldholderclass')->addExtraClass('checkboxfieldextraclass'); + $checkboxsetfield->addHolderClass('checkboxsetfieldholderclass')->addExtraClass('checkboxsetfieldextraclass'); + $dropdownfield->addHolderClass('dropdownfieldholderclass')->addExtraClass('dropdownfieldextraclass'); + $emailfield->addHolderClass('emailfieldholderclass')->addExtraClass('emailfieldextraclass'); + $optionsetfield->addHolderClass('optionsetfieldholderclass')->addExtraClass('optionsetfieldextraclass'); + $phonefield->addHolderClass('phonefieldholderclass')->addExtraClass('phonefieldextraclass'); + $textareafield->addHolderClass('textareafieldholderclass')->addExtraClass('textareafieldextraclass'); + $textfield->addHolderClass('textfieldholderclass')->addExtraClass('textfieldextraclass'); + + $actions = new FieldList(FormAction::create('submit', 'Submit')); + $required = new RequiredFields('required'); + $form = new Form($this, 'Form', $fields, $actions, $required); + + return $form; + } + + /** + * submit - submitted form + * + * @param array $data submitted Data + * @param Form $form original form + * @return mixed + */ + public function submit($data, Form $form) + { + $form->sessionMessage('Hello.', 'success'); + + return $this->redirectBack(); + } +} diff --git a/tests/DemoTest.php b/tests/DemoTest.php deleted file mode 100644 index c54f3af..0000000 --- a/tests/DemoTest.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ -class DemoTest extends SapphireTest -{ - protected static $fixture_file = './fixture.yml'; - - /** - * testDemo - * - * @return void - */ - public function testDemo() - { - $this->assertEquals(2, 1+1); - } -} diff --git a/tests/HolderClassTest.php b/tests/HolderClassTest.php new file mode 100644 index 0000000..3704614 --- /dev/null +++ b/tests/HolderClassTest.php @@ -0,0 +1,81 @@ + + */ +class HolderClassTest extends SapphireTest +{ + protected static $fixture_file = './fixture.yml'; + + /** + * test no title + * + * @return void + */ + public function testNoTitleNoLabel() + { + $field = TextField::create('Field', 'Field'); + $field->setTitle(null); + $this->assertEquals('form-holder--no-label', $field->holderClass()); + } + + /** + * testCheckHolderClass + * + * @return void + */ + public function testCheckHolderClass() + { + $field = TextField::create('Field', 'Field'); + + $field->addHolderClass('holderclass'); + + $this->assertFalse($field->hasHolderClass('test')); + + $this->assertTrue($field->hasHolderClass('holderclass')); + } + + + /** + * testHolderClassManipulation + * + * @return void + */ + public function testHolderClassManipulation() + { + $field = TextField::create('Field', 'Field'); + + $field->addHolderClass('testholderclass'); + $this->assertEquals('testholderclass', $field->holderClass()); + + $field->addHolderClass('secondclass'); + $this->assertEquals('testholderclass secondclass', $field->holderClass()); + + $field->removeHolderClass('testholderclass'); + $this->assertEquals('secondclass', $field->holderClass()); + + $field->removeHolderClass('secondclass'); + $this->assertEquals('', $field->holderClass()); + + } + + /** + * testValidationClass + * + * @return void + */ + public function testValidationClass() + { + $field = TextField::create('Field', 'Field'); + + $field->setMessage('validationError', 'validation'); + + $this->assertStringContainsString('is-invalid', $field->extraClass()); + } +} diff --git a/tests/PhoneFieldTest.php b/tests/PhoneFieldTest.php new file mode 100644 index 0000000..ccc4409 --- /dev/null +++ b/tests/PhoneFieldTest.php @@ -0,0 +1,34 @@ + + */ +class PhoneFieldTest extends FunctionalTest +{ + protected static $fixture_file = './fixture.yml'; + + /** + * testValidation + * + * @return void + */ + public function testValidation() + { + $formPage = $this->objFromFixture(FormPage::class, 'page'); + $formPage->copyVersionToStage('Stage', 'Live'); + + $page = $this->get('/form'); + $submit = $this->submitForm('Form_Form', null, $data = [ + 'phonefield' => '-+asb' + ]); + + $this->assertStringContainsString('Please enter a valid phone number', $submit->getBody()); + } +} diff --git a/tests/RenderFormFieldsTest.php b/tests/RenderFormFieldsTest.php new file mode 100644 index 0000000..c241fe4 --- /dev/null +++ b/tests/RenderFormFieldsTest.php @@ -0,0 +1,184 @@ + + */ +class RenderFormFieldsTest extends FunctionalTest +{ + protected static $fixture_file = './fixture.yml'; + + /** + * By default, the test database won't contain any DataObjects that have the interface TestOnly. + * This variable lets you define additional TestOnly DataObjects to set up for this test. + * Set it to an array of DataObject subclass names. + * + * @var array + */ + protected static $extra_dataobjects = [ + FormPage::class + ]; + + /** + * testCheckboxFieldRendering - description + * + * @return void + */ + public function testCheckboxFieldRendering() + { + $formPage = $this->objFromFixture(FormPage::class, 'page'); + $formPage->copyVersionToStage('Stage', 'Live'); + + $page = $this->get('/form'); + $body = $page->getBody(); + + $this->assertStringContainsString('
', $body); + $this->assertStringContainsString('', $body); + $this->assertStringContainsString('', $body); + } + + /** + * testCheckboxsetFieldRendering - description + * + * @return void + */ + public function testCheckboxsetFieldRendering() + { + $formPage = $this->objFromFixture(FormPage::class, 'page'); + $formPage->copyVersionToStage('Stage', 'Live'); + + $page = $this->get('/form'); + $body = $page->getBody(); + + $this->assertStringContainsString('
', $body); + $this->assertStringContainsString('', $body); + $this->assertStringContainsString('
', $body); + $this->assertStringContainsString('', $body); + } + + /** + * testDropdownFieldRendering - description + * + * @return void + */ + public function testDropdownFieldRendering() + { + $formPage = $this->objFromFixture(FormPage::class, 'page'); + $formPage->copyVersionToStage('Stage', 'Live'); + + $page = $this->get('/form'); + $body = $page->getBody(); + + $this->assertStringContainsString('