Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates for Silverstripe 5 #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions .github/workflows/dispatch-ci.yml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions .github/workflows/keepalive.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 0 additions & 25 deletions .scrutinizer.yml

This file was deleted.

36 changes: 0 additions & 36 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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

Expand Down
1 change: 0 additions & 1 deletion _config.php

This file was deleted.

10 changes: 4 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
"field"
],
"require": {
"silverstripe/framework": "^4.0",
"silverstripe/vendor-plugin": "^1.0",
"silverstripe/framework": "^4.10 || ^5.0",
"silverstripe/vendor-plugin": "^1.0 || ^2.0",
"jakeasmith/http_build_url": "^1"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"extra": {
"installer-name": "externalurlfield"
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.0"
},
"authors": [
{
Expand Down
14 changes: 14 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>

<file>src</file>
<file>tests</file>

<!-- base rules are PSR-2 -->
<rule ref="PSR2" >
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps" />
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />
</rule>
</ruleset>
9 changes: 7 additions & 2 deletions src/ExternalURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function Nice()
unset($parts[$part]);
}

return rtrim(http_build_url($parts), "/");
return http_build_url($parts);
}
}

Expand All @@ -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);
}
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/ExternalURLField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -161,7 +164,7 @@ protected function rebuildURL($url)
}
}

return rtrim(http_build_url($defaults, $parts), "/");
return http_build_url($defaults, $parts);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/ExternalURLFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
22 changes: 22 additions & 0 deletions tests/ExternalURLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down