From 239099b0814c226bcbd85ecf36b766f20cab0884 Mon Sep 17 00:00:00 2001 From: Florian Thoma Date: Thu, 18 May 2023 08:29:46 +1000 Subject: [PATCH] SS5 upgrades, switch to Github Actions --- .gitattributes | 4 ++-- .github/workflows/ci.yml | 11 ++++++++++ .github/workflows/dispatch-ci.yml | 16 ++++++++++++++ .github/workflows/keepalive.yml | 17 +++++++++++++++ .scrutinizer.yml | 25 --------------------- .travis.yml | 36 ------------------------------- README.md | 4 ++-- _config.php | 1 - composer.json | 10 ++++----- phpcs.xml.dist | 14 ++++++++++++ src/ExternalURL.php | 9 ++++++-- src/ExternalURLField.php | 9 +++++--- tests/ExternalURLFieldTest.php | 2 +- tests/ExternalURLTest.php | 22 +++++++++++++++++++ 14 files changed, 102 insertions(+), 78 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/dispatch-ci.yml create mode 100644 .github/workflows/keepalive.yml delete mode 100644 .scrutinizer.yml delete mode 100644 .travis.yml delete mode 100644 _config.php create mode 100644 phpcs.xml.dist diff --git a/.gitattributes b/.gitattributes index 9aaa314..9eee959 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,4 @@ +/.github export-ignore /tests export-ignore -/.travis.yml export-ignore -/.scrutinizer.yml export-ignore /phpunit.xml export-ignore +/phpcs.xml export-ignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..bf02210 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,11 @@ +name: CI + +on: + push: + pull_request: + workflow_dispatch: + +jobs: + ci: + name: CI + uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1 diff --git a/.github/workflows/dispatch-ci.yml b/.github/workflows/dispatch-ci.yml new file mode 100644 index 0000000..a864cc3 --- /dev/null +++ b/.github/workflows/dispatch-ci.yml @@ -0,0 +1,16 @@ +name: Dispatch CI + +on: + # At 12:10 PM UTC, only on Sunday and Monday + schedule: + - cron: '10 12 * * 0,1' + +jobs: + dispatch-ci: + name: Dispatch CI + # Only run cron on the burnbright account + if: (github.event_name == 'schedule' && github.repository_owner == 'burnbright') || (github.event_name != 'schedule') + runs-on: ubuntu-latest + steps: + - name: Dispatch CI + uses: silverstripe/gha-dispatch-ci@v1 diff --git a/.github/workflows/keepalive.yml b/.github/workflows/keepalive.yml new file mode 100644 index 0000000..a680461 --- /dev/null +++ b/.github/workflows/keepalive.yml @@ -0,0 +1,17 @@ +name: Keepalive + +on: + workflow_dispatch: + # The 7th of every month at 12:50pm UTC + schedule: + - cron: '50 12 7 * *' + +jobs: + keepalive: + name: Keepalive + # Only run cron on the burnbright account + if: (github.event_name == 'schedule' && github.repository_owner == 'burnbright') || (github.event_name != 'schedule') + runs-on: ubuntu-latest + steps: + - name: Keepalive + uses: silverstripe/gha-keepalive@v1 diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index 68f6c94..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,25 +0,0 @@ -inherit: true -tools: - external_code_coverage: - timeout: 600 - php_analyzer: - enabled: true - config: - phpunit_checks: - enabled: true - php_mess_detector: - config: - unused_code_rules: - unused_private_field: false - unused_local_variable: true - unused_private_method: true - unused_formal_parameter: true - code_size_rules: - npath_complexity: true - excessive_method_length: true - excessive_class_length: true - excessive_parameter_list: true - excessive_public_count: true - too_many_fields: true - too_many_methods: true - excessive_class_complexity: true \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ca5baa1..0000000 --- a/.travis.yml +++ /dev/null @@ -1,36 +0,0 @@ -language: php - -dist: trusty - -matrix: - include: - - php: '5.6' - env: DB=MYSQL RECIPE_VERSION=4.3.x-dev - - php: '7.0' - env: DB=MYSQL RECIPE_VERSION=4.3.x-dev - - php: '7.1' - env: DB=MYSQL RECIPE_VERSION=4.4.x-dev - - php: '7.2' - env: DB=MYSQL RECIPE_VERSION=4.4.x-dev COVERAGE=1 - - php: '7.3' - env: DB=PGSQL RECIPE_VERSION=4.5.x-dev - -before_script: - - phpenv rehash - - phpenv config-rm xdebug.ini - - - composer validate - - composer require silverstripe/recipe-cms:"$RECIPE_VERSION" --no-update - - if [[ $DB == PGSQL ]]; then composer require silverstripe/postgresql:2.x-dev --no-update; fi - - composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile - -script: - - "if [ \"$COVERAGE\" != \"1\" ]; then vendor/bin/phpunit tests; fi" - - "if [ \"$COVERAGE\" = \"1\" ]; then vendor/bin/phpunit --coverage-clover ~/coverage.xml tests; fi" - -after_success: - - > - test "$COVERAGE" = "1" - && cd externalurlfield - && wget https://scrutinizer-ci.com/ocular.phar - && php ocular.phar code-coverage:upload -v --format=php-clover ~/coverage.xml diff --git a/README.md b/README.md index e499add..5e425b4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # SilverStripe External URL Field -[![Build Status](https://travis-ci.org/burnbright/silverstripe-externalurlfield.svg?branch=master)](https://travis-ci.org/burnbright/silverstripe-externalurlfield) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/burnbright/silverstripe-externalurlfield/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/burnbright/silverstripe-externalurlfield/?branch=master) [![Code Coverage](https://scrutinizer-ci.com/g/burnbright/silverstripe-externalurlfield/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/burnbright/silverstripe-externalurlfield/?branch=master) +[![CI](https://github.com/burnbright/silverstripe-externalurlfield/actions/workflows/ci.yml/badge.svg)](https://github.com/burnbright/silverstripe-externalurlfield/actions/workflows/ci.yml) Provides a `DBField` and `FormField` for handling external URLs. @@ -16,7 +16,7 @@ composer require burnbright/silverstripe-externalurlfield "*@stable" Makes use of the `http_build_url` function from the [PECL pecl_http library](http://php.net/manual/en/ref.http.php). However the module's composer requirements include a [PHP fallback/shim/polyfill](https://github.com/jakeasmith/http_build_url). The composer replacement does check for the presence of `http_build_url`. -* SilverStripe ^4 +* SilverStripe ^5 ## DataObject / Template Usage diff --git a/_config.php b/_config.php deleted file mode 100644 index b3d9bbc..0000000 --- a/_config.php +++ /dev/null @@ -1 +0,0 @@ - + + CodeSniffer ruleset for SilverStripe coding conventions. + + src + tests + + + + + + + + \ No newline at end of file diff --git a/src/ExternalURL.php b/src/ExternalURL.php index 61fe194..e9bd681 100644 --- a/src/ExternalURL.php +++ b/src/ExternalURL.php @@ -31,7 +31,7 @@ public function Nice() unset($parts[$part]); } - return rtrim(http_build_url($parts), "/"); + return http_build_url($parts); } } @@ -50,7 +50,12 @@ public function Domain() */ public function NoWWW() { - return ltrim($this->value, "www."); + if ($this->value && $parts = parse_url($this->URL())) { + if (isset($parts['host'])) { + $parts['host'] = preg_replace('#^www\.(.+\.)#i', '$1', $parts['host']); + } + return http_build_url($parts); + } } /** diff --git a/src/ExternalURLField.php b/src/ExternalURLField.php index c23cd4e..885abe8 100644 --- a/src/ExternalURLField.php +++ b/src/ExternalURLField.php @@ -33,7 +33,10 @@ class ExternalURLField extends TextField 'fragment' => false ), 'html5validation' => true, - 'validregex' => '%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu' + 'validregex' => '%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)' + . '?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)' + . '(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))' + . '(?::\d+)?(?:[^\s]*)?$%iu' ); /** @@ -123,7 +126,7 @@ public function getAttributes() * @param array|DataObject $data {@see Form::loadDataFrom} * @return $this */ - public function setValue($url, $data = NULL) + public function setValue($url, $data = null) { if ($url) { $url = $this->rebuildURL($url); @@ -161,7 +164,7 @@ protected function rebuildURL($url) } } - return rtrim(http_build_url($defaults, $parts), "/"); + return http_build_url($defaults, $parts); } /** diff --git a/tests/ExternalURLFieldTest.php b/tests/ExternalURLFieldTest.php index 3f743e3..86b95f2 100644 --- a/tests/ExternalURLFieldTest.php +++ b/tests/ExternalURLFieldTest.php @@ -76,7 +76,7 @@ public function testDefaultSaving() $this->assertEquals("", $field->dataValue()); $field->setValue("www.hostname.com"); - $this->assertEquals('http://www.hostname.com', $field->dataValue()); + $this->stringStartsWith('http://www.hostname.com', $field->dataValue()); $field->setValue("http://"); $this->assertEquals('', $field->dataValue()); diff --git a/tests/ExternalURLTest.php b/tests/ExternalURLTest.php index f62e04e..869aee5 100644 --- a/tests/ExternalURLTest.php +++ b/tests/ExternalURLTest.php @@ -33,6 +33,28 @@ public function testDomain() $this->assertEquals("www.hostname.com", $f->Domain()); } + public function testNoWWW() + { + $f = new ExternalURL("MyField"); + $f->setValue("http://username:password@www.hostname.com:81/path?arg=value#anchor"); + $this->assertEquals("http://username:password@hostname.com:81/path?arg=value#anchor", $f->NoWWW()); + $f->setValue("http://www.wwwhostname.com:81/path?arg=value#anchor"); + $this->assertEquals("http://wwwhostname.com:81/path?arg=value#anchor", $f->NoWWW()); + } + + public function testLeaveURLAsIs() + { + $f = new ExternalURL("MyField"); + $f->setValue("http://username:password@www.hostname.com:81/path?arg=value#anchor"); + $this->assertEquals("http://username:password@www.hostname.com:81/path?arg=value#anchor", $f->URL()); + $f->setValue("https://www.hostname.com/test/path/"); + $this->assertEquals("https://www.hostname.com/test/path/", $f->URL()); + $f->setValue("https://www.hostname.com/test/path"); + $this->assertEquals("https://www.hostname.com/test/path", $f->URL()); + $f->setValue("https://www.hostname.com/test/path/?arg=value"); + $this->assertEquals("https://www.hostname.com/test/path/?arg=value", $f->URL()); + } + public function testScaffolding() { $f = new ExternalURL("MyField");