API Upgrade SapphireTest to work with phpunit 9 (#10028)

This commit is contained in:
Steve Boyd 2021-10-27 15:39:47 +13:00 committed by GitHub
parent 059d8aac0a
commit cd076542f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
165 changed files with 3300 additions and 1264 deletions

View File

@ -12,7 +12,7 @@ env:
jobs:
fast_finish: true
include:
- php: 7.1
- php: 7.3
env:
- DB=MYSQL
- REQUIRE_INSTALLER="$REQUIRE_RECIPE"
@ -20,21 +20,7 @@ jobs:
- PHPUNIT_TEST=1
- PHPUNIT_SUITE="framework"
- COMPOSER_INSTALL_ARG="--prefer-lowest"
- php: 7.2
env:
- DB=PGSQL
- PDO=1
- REQUIRE_INSTALLER="$REQUIRE_RECIPE"
- PHPUNIT_TEST=1
- PHPUNIT_TEST="framework"
- php: 7.3
env:
- DB=MYSQL
- PDO=1
- REQUIRE_INSTALLER="$REQUIRE_RECIPE"
- PHPUNIT_TEST=1
- PHPUNIT_SUITE="framework"
- php: 7.3
- php: 7.4
env:
- DB=MYSQL
- REQUIRE_INSTALLER="$REQUIRE_RECIPE"
@ -46,10 +32,9 @@ jobs:
- REQUIRE_INSTALLER="$REQUIRE_RECIPE"
- PHPUNIT_TEST=1
- PHPUNIT_SUITE="framework"
- php: nightly
- php: 8.0
env:
- DB=MYSQL
- REQUIRE_INSTALLER="$REQUIRE_RECIPE"
- PHPUNIT_TEST=1
- PHPUNIT_SUITE="framework"
- COMPOSER_INSTALL_ARG="--ignore-platform-reqs"

View File

@ -39,7 +39,7 @@
"symfony/config": "^3.2 || ^4",
"symfony/translation": "^2.8 || ^3 || ^4",
"symfony/yaml": "^3.2 || ^4",
"php": "^7.1 || ^8",
"php": "^7.3 || ^8",
"ext-ctype": "*",
"ext-dom": "*",
"ext-hash": "*",
@ -52,11 +52,14 @@
"ext-xml": "*"
},
"require-dev": {
"sminnee/phpunit": "^5.7.29",
"sminnee/phpunit-mock-objects": "^3.4.9",
"phpunit/phpunit": "^9.5",
"dms/phpunit-arraysubset-asserts": "^0.3.0",
"silverstripe/versioned": "^1",
"squizlabs/php_codesniffer": "^3.5"
},
"conflict": {
"phpunit/phpunit": "^6 || ^7 || ^8"
},
"provide": {
"psr/container-implementation": "1.0.0"
},

View File

@ -56,7 +56,7 @@ you want to test a `Controller`, `Form` or anything that requires a web page.
`FunctionalTest` is a subclass of `SapphireTest` so will inherit all of the behaviors. By subclassing `FunctionalTest`
you gain the ability to load and test web pages on the site.
`SapphireTest` in turn, extends `PHPUnit_Framework_TestCase`. For more information on `PHPUnit_Framework_TestCase` see
`SapphireTest` in turn, extends `PHPUnit\Framework\TestCase`. For more information on `PHPUnit\Framework\TestCase` see
the [PHPUnit](http://www.phpunit.de) documentation. It provides a lot of fundamental concepts that we build on in this
documentation.
[/info]
@ -89,9 +89,11 @@ needs.
```xml
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>app/tests</directory>
</testsuite>
<testsuites>
<testsuite name="Default">
<directory>app/tests</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>sanitychecks</group>
@ -115,7 +117,7 @@ class PageTest extends SapphireTest
{
protected $usesDatabase = true;
public function setUp()
protected function setUp(): void
{
parent::setUp();
@ -152,14 +154,14 @@ use SilverStripe\Dev\SapphireTest;
class PageTest extends SapphireTest
{
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
// ..
}
public static function tearDownAfterClass()
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
@ -180,7 +182,7 @@ It's important to remember that the `parent::setUp();` functions will need to be
```php
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
//this will remain for the whole suite and be removed for any other tests

View File

@ -65,9 +65,11 @@ Page:
```xml
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>app/tests/</directory>
</testsuite>
<testsuites>
<testsuite name="Default">
<directory>app/tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">app/src</directory>

View File

@ -1,18 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Standard module phpunit configuration.
Requires PHPUnit ^5.7
Requires PHPUnit ^9
-->
<phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
<testsuite name="framework">
<directory>tests/php</directory>
</testsuite>
<testsuite name="cms">
<directory>vendor/silverstripe/cms/tests</directory>
</testsuite>
<testsuites>
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
<testsuite name="framework">
<directory>tests/php</directory>
</testsuite>
<testsuite name="cms">
<directory>vendor/silverstripe/cms/tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">.</directory>

View File

@ -4,11 +4,165 @@ namespace SilverStripe\Dev\Constraint;
use PHPUnit_Framework_Constraint;
use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use SilverStripe\Dev\SSListExporter;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\SS_List;
use SilverStripe\View\ViewableData;
/* -------------------------------------------------
*
* This version of SSListContains is for phpunit 9
* The phpunit 5 version is lower down in this file
* phpunit 6, 7 and 8 are not supported
*
* @see SilverStripe\Dev\SapphireTest
*
* -------------------------------------------------
*/
if (class_exists(Constraint::class)) {
/**
* Constraint for checking if a SS_List contains items matching the given
* key-value pairs.
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class SSListContains extends Constraint implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* @var array
*/
protected $matches = [];
/**
* Check if the list has left over items that don't match
*
* @var bool
*/
protected $hasLeftoverItems = false;
public function __construct(array $matches)
{
$this->exporter = new SSListExporter();
$this->matches = $matches;
}
/**
* Evaluates the constraint for parameter $other
*
* If $returnResult is set to false (the default), an exception is thrown
* in case of a failure. null is returned otherwise.
*
* If $returnResult is true, the result of the evaluation is returned as
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param SS_List $other Value or object to evaluate.
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @return null|bool
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false): ?bool
{
$success = true;
foreach ($other as $item) {
$this->checkIfItemEvaluatesRemainingMatches($item);
}
//we have remaining matches?
if (count($this->matches) !== 0) {
$success = false;
$this->hasLeftoverItems = true;
}
if ($returnResult) {
return $success;
}
if (!$success) {
$this->fail($other, $description);
}
return null;
}
/**
* @param ViewableData $item
* @return bool
*/
protected function checkIfItemEvaluatesRemainingMatches(ViewableData $item): bool
{
$success = false;
foreach ($this->matches as $key => $match) {
$constraint = new ViewableDataContains($match);
if ($constraint->evaluate($item, '', true)) {
$success = true;
unset($this->matches[$key]);
break;
}
}
return $success;
}
/**
* Returns a string representation of the object.
*
* @return string
*/
public function toString(): string
{
$matchToString = function ($key, $value) {
return ' "' . $key . '" is "' . $value . '"';
};
$matchesToString = function ($matches) use ($matchToString) {
$matchesAsString = implode(' and ', array_map(
$matchToString,
array_keys($matches),
array_values($matches)
));
return '(' . $matchesAsString . ')';
};
$allMatchesAsString = implode(
"\n or ",
array_map($matchesToString, $this->matches)
);
return $this->getStubForToString() . $allMatchesAsString;
}
/**
* @return string
*/
protected function getStubForToString(): string
{
return ' contains an item matching ';
}
}
}
/* -------------------------------------------------
*
* This version of FunctionalTest is for phpunit 5
* The phpunit 9 verison is at the top of this file
*
* -------------------------------------------------
*/
if (!class_exists(PHPUnit_Framework_Constraint::class)) {
return;
}
@ -17,8 +171,11 @@ if (!class_exists(PHPUnit_Framework_Constraint::class)) {
* Constraint for checking if a SS_List contains items matching the given
* key-value pairs.
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class SSListContains extends PHPUnit_Framework_Constraint implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* @var array
*/
@ -33,7 +190,6 @@ class SSListContains extends PHPUnit_Framework_Constraint implements TestOnly
public function __construct($matches)
{
parent::__construct();
$this->exporter = new SSListExporter();
$this->matches = $matches;
@ -55,7 +211,7 @@ class SSListContains extends PHPUnit_Framework_Constraint implements TestOnly
*
* @return null|bool
*
* @throws PHPUnit_Framework_ExpectationFailedException
* @throws ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false)
{
@ -132,6 +288,9 @@ class SSListContains extends PHPUnit_Framework_Constraint implements TestOnly
return $this->getStubForToString() . $allMatchesAsString;
}
/**
* @return string
*/
protected function getStubForToString()
{
return ' contains an item matching ';

View File

@ -4,10 +4,21 @@ namespace SilverStripe\Dev\Constraint;
use PHPUnit_Framework_Constraint;
use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\SS_List;
if (!class_exists(PHPUnit_Framework_Constraint::class)) {
/* -------------------------------------------------
*
* This version of SSListContains is for both phpunit5 and phpunit 9 because it extends SSListContains
* phpunit 6, 7 and 8 are not supported
*
* @see SilverStripe\Dev\SapphireTest
*
* -------------------------------------------------
*/
if (!class_exists(Constraint::class) && !class_exists(PHPUnit_Framework_Constraint::class)) {
return;
}
@ -40,9 +51,9 @@ class SSListContainsOnly extends SSListContains implements TestOnly
*
* @return null|bool
*
* @throws PHPUnit_Framework_ExpectationFailedException
* @throws PHPUnit_Framework_ExpectationFailedException|ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false)
public function evaluate($other, $description = '', $returnResult = false): ?bool
{
$success = true;
@ -71,7 +82,7 @@ class SSListContainsOnly extends SSListContains implements TestOnly
return null;
}
protected function getStubForToString()
protected function getStubForToString(): string
{
return $this->itemNotMatching
? parent::getStubForToString()

View File

@ -4,20 +4,125 @@ namespace SilverStripe\Dev\Constraint;
use PHPUnit_Framework_Constraint;
use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use SilverStripe\Dev\SSListExporter;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\SS_List;
/* -------------------------------------------------
*
* This version of SSListContainsOnlyMatchingItems is for phpunit 9
* The phpunit 5 version is lower down in this file
* phpunit 6, 7 and 8 are not supported
*
* @see SilverStripe\Dev\SapphireTest
*
* -------------------------------------------------
*/
if (class_exists(Constraint::class)) {
/**
* Constraint for checking if every item in a SS_List matches a given match,
* e.g. every Member has isActive set to true
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class SSListContainsOnlyMatchingItems extends Constraint implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* @var array
*/
private $match;
/**
* @var ViewableDataContains
*/
private $constraint;
public function __construct($match)
{
$this->exporter = new SSListExporter();
$this->constraint = new ViewableDataContains($match);
$this->match = $match;
}
/**
* Evaluates the constraint for parameter $other
*
* If $returnResult is set to false (the default), an exception is thrown
* in case of a failure. null is returned otherwise.
*
* If $returnResult is true, the result of the evaluation is returned as
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param SS_List $other Value or object to evaluate.
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @return null|bool
*
* @throws ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false): ?bool
{
$success = true;
foreach ($other as $item) {
if (!$this->constraint->evaluate($item, '', true)) {
$success = false;
break;
}
}
if ($returnResult) {
return $success;
}
if (!$success) {
$this->fail($other, $description);
}
return null;
}
/**
* Returns a string representation of the object.
*
* @return string
*/
public function toString(): string
{
return 'contains only Objects where "' . key($this->match) . '" is "' . current($this->match) . '"';
}
}
}
if (!class_exists(PHPUnit_Framework_Constraint::class)) {
return;
}
/* -------------------------------------------------
*
* This version of SSListContainsOnlyMatchingItems is for phpunit 5
* The phpunit 9 verison is at the top of this file
*
* -------------------------------------------------
*/
/**
* Constraint for checking if every item in a SS_List matches a given match,
* e.g. every Member has isActive set to true
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class SSListContainsOnlyMatchingItems extends PHPUnit_Framework_Constraint implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* @var array
*/
@ -30,7 +135,6 @@ class SSListContainsOnlyMatchingItems extends PHPUnit_Framework_Constraint imple
public function __construct($match)
{
parent::__construct();
$this->exporter = new SSListExporter();
$this->constraint = new ViewableDataContains($match);

View File

@ -5,8 +5,117 @@ namespace SilverStripe\Dev\Constraint;
use PHPUnit_Framework_Constraint;
use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit_Util_InvalidArgumentHelper;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use SilverStripe\Dev\TestOnly;
use SilverStripe\View\ViewableData;
use SilverStripe\Dev\SapphireTest;
/* -------------------------------------------------
*
* This version of ViewableDataContains is for phpunit 9
* The phpunit 5 version is lower down in this file
* phpunit 6, 7 and 8 are not supported
*
* @see SilverStripe\Dev\SapphireTest
*
* -------------------------------------------------
*/
if (class_exists(Constraint::class)) {
/**
* Constraint for checking if a ViewableData (e.g. ArrayData or any DataObject) contains fields matching the given
* key-value pairs.
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class ViewableDataContains extends Constraint implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* @var array
*/
private $match;
/**
* ViewableDataContains constructor.
* @param array $match
*/
public function __construct(array $match)
{
if (!is_array($match)) {
throw SapphireTest::createInvalidArgumentException(
1,
'array'
);
}
$this->match = $match;
}
/**
* Evaluates the constraint for parameter $other
*
* If $returnResult is set to false (the default), an exception is thrown
* in case of a failure. null is returned otherwise.
*
* If $returnResult is true, the result of the evaluation is returned as
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @param ViewableData $other Value or object to evaluate.
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
*
* @return null|bool
*
* @throws ExpectationFailedException
*/
public function evaluate($other, $description = '', $returnResult = false): ?bool
{
$success = true;
foreach ($this->match as $fieldName => $value) {
if ($other->$fieldName != $value) {
$success = false;
break;
}
}
if ($returnResult) {
return $success;
}
if (!$success) {
$this->fail($other, $description);
}
return null;
}
/**
* Returns a string representation of the object.
*
* @todo: add representation for more than one match
*
* @return string
*/
public function toString(): string
{
return 'contains only Objects where "' . key($this->match) . '" is "' . current($this->match) . '"';
}
}
}
/* -------------------------------------------------
*
* This version of ViewableDataContains is for phpunit 5
* The phpunit 9 verison is at the top of this file
*
* -------------------------------------------------
*/
if (!class_exists(PHPUnit_Framework_Constraint::class)) {
return;
@ -16,8 +125,11 @@ if (!class_exists(PHPUnit_Framework_Constraint::class)) {
* Constraint for checking if a ViewableData (e.g. ArrayData or any DataObject) contains fields matching the given
* key-value pairs.
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class ViewableDataContains extends PHPUnit_Framework_Constraint implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* @var array
*/
@ -30,7 +142,6 @@ class ViewableDataContains extends PHPUnit_Framework_Constraint implements TestO
public function __construct($match)
{
parent::__construct();
if (!is_array($match)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(
1,
@ -81,7 +192,6 @@ class ViewableDataContains extends PHPUnit_Framework_Constraint implements TestO
return null;
}
/**
* Returns a string representation of the object.
*

View File

@ -3,6 +3,9 @@
namespace SilverStripe\Dev;
use PHPUnit_Framework_AssertionFailedError;
use PHPUnit_Extensions_GroupTestSuite;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Constraint\IsEqualCanonicalizing;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\Session;
@ -12,6 +15,463 @@ use SilverStripe\Security\SecurityToken;
use SilverStripe\View\SSViewer;
use SimpleXMLElement;
/* -------------------------------------------------
*
* This version of FunctionalTest is for phpunit 9
* The phpunit 5 version is lower down in this file
* phpunit 6, 7 and 8 are not supported
*
* @see SilverStripe\Dev\SapphireTest
*
* IsEqualCanonicalizing::class is a new class added in PHPUnit 9, testing that this class exists
* to ensure that we're not using a a prior, incompatible version of PHPUnit
*
* -------------------------------------------------
*/
if (class_exists(IsEqualCanonicalizing::class)) {
/**
* SilverStripe-specific testing object designed to support functional testing of your web app. It simulates get/post
* requests, form submission, and can validate resulting HTML, looking up content by CSS selector.
*
* The example below shows how it works.
*
* <code>
* public function testMyForm() {
* // Visit a URL
* $this->get("your/url");
*
* // Submit a form on the page that you get in response
* $this->submitForm("MyForm_ID", "action_dologin", array("Email" => "invalid email ^&*&^"));
*
* // Validate the content that is returned
* $this->assertExactMatchBySelector("#MyForm_ID p.error", array("That email address is invalid."));
* }
* </code>
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class FunctionalTest extends SapphireTest implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* Set this to true on your sub-class to disable the use of themes in this test.
* This can be handy for functional testing of modules without having to worry about whether a user has changed
* behaviour by replacing the theme.
*
* @var bool
*/
protected static $disable_themes = false;
/**
* Set this to true on your sub-class to use the draft site by default for every test in this class.
*
* @deprecated 4.2.0:5.0.0 Use ?stage=Stage in your ->get() querystring requests instead
* @var bool
*/
protected static $use_draft_site = false;
/**
* @var TestSession
*/
protected $mainSession = null;
/**
* CSSContentParser for the most recently requested page.
*
* @var CSSContentParser
*/
protected $cssParser = null;
/**
* If this is true, then 30x Location headers will be automatically followed.
* If not, then you will have to manaully call $this->mainSession->followRedirection() to follow them.
* However, this will let you inspect the intermediary headers
*
* @var bool
*/
protected $autoFollowRedirection = true;
/**
* Returns the {@link Session} object for this test
*
* @return Session
*/
public function session()
{
return $this->mainSession->session();
}
protected function setUp(): void
{
parent::setUp();
// Skip calling FunctionalTest directly.
if (static::class == __CLASS__) {
$this->markTestSkipped(sprintf('Skipping %s ', static::class));
}
$this->mainSession = new TestSession();
// Disable theme, if necessary
if (static::get_disable_themes()) {
SSViewer::config()->update('theme_enabled', false);
}
// Flush user
$this->logOut();
// Switch to draft site, if necessary
// If you rely on this you should be crafting stage-specific urls instead though.
if (static::get_use_draft_site()) {
$this->useDraftSite();
}
// Unprotect the site, tests are running with the assumption it's off. They will enable it on a case-by-case
// basis.
BasicAuth::protect_entire_site(false);
SecurityToken::disable();
}
protected function tearDown(): void
{
SecurityToken::enable();
unset($this->mainSession);
parent::tearDown();
}
/**
* Run a test while mocking the base url with the provided value
* @param string $url The base URL to use for this test
* @param callable $callback The test to run
*/
protected function withBaseURL($url, $callback)
{
$oldBase = Config::inst()->get(Director::class, 'alternate_base_url');
Config::modify()->set(Director::class, 'alternate_base_url', $url);
$callback($this);
Config::modify()->set(Director::class, 'alternate_base_url', $oldBase);
}
/**
* Run a test while mocking the base folder with the provided value
* @param string $folder The base folder to use for this test
* @param callable $callback The test to run
*/
protected function withBaseFolder($folder, $callback)
{
$oldFolder = Config::inst()->get(Director::class, 'alternate_base_folder');
Config::modify()->set(Director::class, 'alternate_base_folder', $folder);
$callback($this);
Config::modify()->set(Director::class, 'alternate_base_folder', $oldFolder);
}
/**
* Submit a get request
* @uses Director::test()
*
* @param string $url
* @param Session $session
* @param array $headers
* @param array $cookies
* @return HTTPResponse
*/
public function get($url, $session = null, $headers = null, $cookies = null)
{
$this->cssParser = null;
$response = $this->mainSession->get($url, $session, $headers, $cookies);
if ($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) {
$response = $this->mainSession->followRedirection();
}
return $response;
}
/**
* Submit a post request
*
* @uses Director::test()
* @param string $url
* @param array $data
* @param array $headers
* @param Session $session
* @param string $body
* @param array $cookies
* @return HTTPResponse
*/
public function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null)
{
$this->cssParser = null;
$response = $this->mainSession->post($url, $data, $headers, $session, $body, $cookies);
if ($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) {
$response = $this->mainSession->followRedirection();
}
return $response;
}
/**
* Submit the form with the given HTML ID, filling it out with the given data.
* Acts on the most recent response.
*
* Any data parameters have to be present in the form, with exact form field name
* and values, otherwise they are removed from the submission.
*
* Caution: Parameter names have to be formatted
* as they are in the form submission, not as they are interpreted by PHP.
* Wrong: array('mycheckboxvalues' => array(1 => 'one', 2 => 'two'))
* Right: array('mycheckboxvalues[1]' => 'one', 'mycheckboxvalues[2]' => 'two')
*
* @see http://www.simpletest.org/en/form_testing_documentation.html
*
* @param string $formID HTML 'id' attribute of a form (loaded through a previous response)
* @param string $button HTML 'name' attribute of the button (NOT the 'id' attribute)
* @param array $data Map of GET/POST data.
* @return HTTPResponse
*/
public function submitForm($formID, $button = null, $data = [])
{
$this->cssParser = null;
$response = $this->mainSession->submitForm($formID, $button, $data);
if ($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) {
$response = $this->mainSession->followRedirection();
}
return $response;
}
/**
* Return the most recent content
*
* @return string
*/
public function content()
{
return $this->mainSession->lastContent();
}
/**
* Find an attribute in a SimpleXMLElement object by name.
* @param SimpleXMLElement $object
* @param string $attribute Name of attribute to find
* @return SimpleXMLElement object of the attribute
*/
public function findAttribute($object, $attribute)
{
$found = false;
foreach ($object->attributes() as $a => $b) {
if ($a == $attribute) {
$found = $b;
}
}
return $found;
}
/**
* Return a CSSContentParser for the most recent content.
*
* @return CSSContentParser
*/
public function cssParser()
{
if (!$this->cssParser) {
$this->cssParser = new CSSContentParser($this->mainSession->lastContent());
}
return $this->cssParser;
}
/**
* Assert that the most recently queried page contains a number of content tags specified by a CSS selector.
* The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag
* will be examined. The assertion fails if one of the expectedMatches fails to appear.
*
* Note: &nbsp; characters are stripped from the content; make sure that your assertions take this into account.
*
* @param string $selector A basic CSS selector, e.g. 'li.jobs h3'
* @param array|string $expectedMatches The content of at least one of the matched tags
* @param string $message
* @throws AssertionFailedError
*/
public function assertPartialMatchBySelector($selector, $expectedMatches, $message = null)
{
if (is_string($expectedMatches)) {
$expectedMatches = [$expectedMatches];
}
$items = $this->cssParser()->getBySelector($selector);
$actuals = [];
if ($items) {
foreach ($items as $item) {
$actuals[trim(preg_replace('/\s+/', ' ', (string)$item))] = true;
}
}
$message = $message ?:
"Failed asserting the CSS selector '$selector' has a partial match to the expected elements:\n'"
. implode("'\n'", $expectedMatches) . "'\n\n"
. "Instead the following elements were found:\n'" . implode("'\n'", array_keys($actuals)) . "'";
foreach ($expectedMatches as $match) {
$this->assertTrue(isset($actuals[$match]), $message);
}
}
/**
* Assert that the most recently queried page contains a number of content tags specified by a CSS selector.
* The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag
* will be examined. The assertion fails if one of the expectedMatches fails to appear.
*
* Note: &nbsp; characters are stripped from the content; make sure that your assertions take this into account.
*
* @param string $selector A basic CSS selector, e.g. 'li.jobs h3'
* @param array|string $expectedMatches The content of *all* matching tags as an array
* @param string $message
* @throws AssertionFailedError
*/
public function assertExactMatchBySelector($selector, $expectedMatches, $message = null)
{
if (is_string($expectedMatches)) {
$expectedMatches = [$expectedMatches];
}
$items = $this->cssParser()->getBySelector($selector);
$actuals = [];
if ($items) {
foreach ($items as $item) {
$actuals[] = trim(preg_replace('/\s+/', ' ', (string)$item));
}
}
$message = $message ?:
"Failed asserting the CSS selector '$selector' has an exact match to the expected elements:\n'"
. implode("'\n'", $expectedMatches) . "'\n\n"
. "Instead the following elements were found:\n'" . implode("'\n'", $actuals) . "'";
$this->assertTrue($expectedMatches == $actuals, $message);
}
/**
* Assert that the most recently queried page contains a number of content tags specified by a CSS selector.
* The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag
* will be examined. The assertion fails if one of the expectedMatches fails to appear.
*
* Note: &nbsp; characters are stripped from the content; make sure that your assertions take this into account.
*
* @param string $selector A basic CSS selector, e.g. 'li.jobs h3'
* @param array|string $expectedMatches The content of at least one of the matched tags
* @param string $message
* @throws AssertionFailedError
*/
public function assertPartialHTMLMatchBySelector($selector, $expectedMatches, $message = null)
{
if (is_string($expectedMatches)) {
$expectedMatches = [$expectedMatches];
}
$items = $this->cssParser()->getBySelector($selector);
$actuals = [];
if ($items) {
/** @var SimpleXMLElement $item */
foreach ($items as $item) {
$actuals[$item->asXML()] = true;
}
}
$message = $message ?:
"Failed asserting the CSS selector '$selector' has a partial match to the expected elements:\n'"
. implode("'\n'", $expectedMatches) . "'\n\n"
. "Instead the following elements were found:\n'" . implode("'\n'", array_keys($actuals)) . "'";
foreach ($expectedMatches as $match) {
$this->assertTrue(isset($actuals[$match]), $message);
}
}
/**
* Assert that the most recently queried page contains a number of content tags specified by a CSS selector.
* The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag
* will be examined. The assertion fails if one of the expectedMatches fails to appear.
*
* Note: &nbsp; characters are stripped from the content; make sure that your assertions take this into account.
*
* @param string $selector A basic CSS selector, e.g. 'li.jobs h3'
* @param array|string $expectedMatches The content of *all* matched tags as an array
* @param string $message
* @throws AssertionFailedError
*/
public function assertExactHTMLMatchBySelector($selector, $expectedMatches, $message = null)
{
$items = $this->cssParser()->getBySelector($selector);
$actuals = [];
if ($items) {
/** @var SimpleXMLElement $item */
foreach ($items as $item) {
$actuals[] = $item->asXML();
}
}
$message = $message ?:
"Failed asserting the CSS selector '$selector' has an exact match to the expected elements:\n'"
. implode("'\n'", $expectedMatches) . "'\n\n"
. "Instead the following elements were found:\n'" . implode("'\n'", $actuals) . "'";
$this->assertTrue($expectedMatches == $actuals, $message);
}
/**
* Use the draft (stage) site for testing.
* This is helpful if you're not testing publication functionality and don't want "stage management" cluttering
* your test.
*
* @deprecated 4.2.0:5.0.0 Use ?stage=Stage querystring arguments instead of useDraftSite
* @param bool $enabled toggle the use of the draft site
*/
public function useDraftSite($enabled = true)
{
Deprecation::notice('5.0', 'Use ?stage=Stage querystring arguments instead of useDraftSite');
if ($enabled) {
$this->session()->set('readingMode', 'Stage.Stage');
$this->session()->set('unsecuredDraftSite', true);
} else {
$this->session()->clear('readingMode');
$this->session()->clear('unsecuredDraftSite');
}
}
/**
* @return bool
*/
public static function get_disable_themes()
{
return static::$disable_themes;
}
/**
* @deprecated 4.2.0:5.0.0 Use ?stage=Stage in your querystring arguments instead
* @return bool
*/
public static function get_use_draft_site()
{
return static::$use_draft_site;
}
}
}
/* -------------------------------------------------
*
* This version of FunctionalTest is for PHPUnit 5
* The PHPUnit 9 version is at the top of this file
*
* PHPUnit_Extensions_GroupTestSuite is a class that only exists in PHPUnit 5
*
* -------------------------------------------------
*/
if (!class_exists(PHPUnit_Extensions_GroupTestSuite::class)) {
return;
}
/**
* SilverStripe-specific testing object designed to support functional testing of your web app. It simulates get/post
* requests, form submission, and can validate resulting HTML, looking up content by CSS selector.
@ -31,8 +491,11 @@ use SimpleXMLElement;
* }
* </code>
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class FunctionalTest extends SapphireTest implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* Set this to true on your sub-class to disable the use of themes in this test.
* This can be handy for functional testing of modules without having to worry about whether a user has changed

View File

@ -1,12 +1,21 @@
<?php
namespace SilverStripe\Dev;
use Exception;
use LogicException;
use PHPUnit_Framework_Constraint_Not;
use PHPUnit_Extensions_GroupTestSuite;
use PHPUnit_Framework_Error;
use PHPUnit_Framework_Error_Warning;
use PHPUnit_Framework_Error_Notice;
use PHPUnit_Framework_Error_Deprecation;
use PHPUnit_Framework_TestCase;
use PHPUnit_Util_InvalidArgumentHelper;
use PHPUnit\Framework\Constraint\LogicalNot;
use PHPUnit\Framework\Constraint\IsEqualCanonicalizing;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Exception as PHPUnitFrameworkException;
use PHPUnit\Util\Test as TestUtil;
use SilverStripe\CMS\Controllers\RootURLController;
use SilverStripe\Control\CLIRequestBuilder;
use SilverStripe\Control\Controller;
@ -39,7 +48,1319 @@ use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\View\SSViewer;
if (!class_exists(PHPUnit_Framework_TestCase::class)) {
/* -------------------------------------------------
*
* This version of SapphireTest is for phpunit 9
* The PHPUnit 5 version is lower down in this file
* PHPUnit 6, 7 and 8 are not supported
*
* Why there are two versions of SapphireTest:
* - PHPUnit 5 is not compatible with PHP 8
* - a mimimum versin of PHP 7.3 is required for PHPUnit 9
*
* The PHPUnit-5 compatibility layer will be preserved until support for PHP7 is dropped in early 2023.
*
* The used on `if(class_exists()` and indentation ensure the the phpunit 5 version function
* signature is used by the php interprester. This is required because the phpunit5
* signature for `setUp()` has no return type while the phpunit 9 version has `setUp(): void`
* Return type covariance allows more specific return types to be defined, while contravariance
* of return types of more abstract return types is not supported
*
* IsEqualCanonicalizing::class is a new class added in PHPUnit 9, testing that this class exists
*
* -------------------------------------------------
*/
if (class_exists(IsEqualCanonicalizing::class)) {
/**
* Test case class for the Sapphire framework.
* Sapphire unit testing is based on PHPUnit, but provides a number of hooks into our data model that make it easier
* to work with.
*
* This class should not be used anywhere outside of unit tests, as phpunit may not be installed
* in production sites.
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class SapphireTest extends TestCase implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* Path to fixture data for this test run.
* If passed as an array, multiple fixture files will be loaded.
* Please note that you won't be able to refer with "=>" notation
* between the fixtures, they act independent of each other.
*
* @var string|array
*/
protected static $fixture_file = null;
/**
* @deprecated 4.0..5.0 Use FixtureTestState instead
* @var FixtureFactory
*/
protected $fixtureFactory;
/**
* @var Boolean If set to TRUE, this will force a test database to be generated
* in {@link setUp()}. Note that this flag is overruled by the presence of a
* {@link $fixture_file}, which always forces a database build.
*
* @var bool
*/
protected $usesDatabase = null;
/**
* This test will cleanup its state via transactions.
* If set to false a full schema is forced between tests, but at a performance cost.
*
* @var bool
*/
protected $usesTransactions = true;
/**
* @var bool
*/
protected static $is_running_test = false;
/**
* By default, setUp() does not require default records. Pass
* class names in here, and the require/augment default records
* function will be called on them.
*
* @var array
*/
protected $requireDefaultRecordsFrom = [];
/**
* A list of extensions that can't be applied during the execution of this run. If they are
* applied, they will be temporarily removed and a database migration called.
*
* The keys of the are the classes that the extensions can't be applied the extensions to, and
* the values are an array of illegal extensions on that class.
*
* Set a class to `*` to remove all extensions (unadvised)
*
* @var array
*/
protected static $illegal_extensions = [];
/**
* A list of extensions that must be applied during the execution of this run. If they are
* not applied, they will be temporarily added and a database migration called.
*
* The keys of the are the classes to apply the extensions to, and the values are an array
* of required extensions on that class.
*
* Example:
* <code>
* array("MyTreeDataObject" => array("Versioned", "Hierarchy"))
* </code>
*
* @var array
*/
protected static $required_extensions = [];
/**
* 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 = [];
/**
* List of class names of {@see Controller} objects to register routes for
* Controllers must implement Link() method
*
* @var array
*/
protected static $extra_controllers = [];
/**
* We need to disabling backing up of globals to avoid overriding
* the few globals SilverStripe relies on, like $lang for the i18n subsystem.
*
* @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html
*/
protected $backupGlobals = false;
/**
* State management container for SapphireTest
*
* @var SapphireTestState
*/
protected static $state = null;
/**
* Temp database helper
*
* @var TempDatabase
*/
protected static $tempDB = null;
/**
* @return TempDatabase
*/
public static function tempDB()
{
if (!class_exists(TempDatabase::class)) {
return null;
}
if (!static::$tempDB) {
static::$tempDB = TempDatabase::create();
}
return static::$tempDB;
}
/**
* Gets illegal extensions for this class
*
* @return array
*/
public static function getIllegalExtensions()
{
return static::$illegal_extensions;
}
/**
* Gets required extensions for this class
*
* @return array
*/
public static function getRequiredExtensions()
{
return static::$required_extensions;
}
/**
* Check if test bootstrapping has been performed. Must not be relied on
* outside of unit tests.
*
* @return bool
*/
protected static function is_running_test()
{
return self::$is_running_test;
}
/**
* Set test running state
*
* @param bool $bool
*/
protected static function set_is_running_test($bool)
{
self::$is_running_test = $bool;
}
/**
* @return String
*/
public static function get_fixture_file()
{
return static::$fixture_file;
}
/**
* @return bool
*/
public function getUsesDatabase()
{
return $this->usesDatabase;
}
/**
* @return bool
*/
public function getUsesTransactions()
{
return $this->usesTransactions;
}
/**
* @return array
*/
public function getRequireDefaultRecordsFrom()
{
return $this->requireDefaultRecordsFrom;
}
/**
* Setup the test.
* Always sets up in order:
* - Reset php state
* - Nest
* - Custom state helpers
*
* User code should call parent::setUp() before custom setup code
*/
protected function setUp(): void
{
if (!defined('FRAMEWORK_PATH')) {
trigger_error(
'Missing constants, did you remember to include the test bootstrap in your phpunit.xml file?',
E_USER_WARNING
);
}
// Call state helpers
static::$state->setUp($this);
// We cannot run the tests on this abstract class.
if (static::class == __CLASS__) {
$this->markTestSkipped(sprintf('Skipping %s ', static::class));
}
// i18n needs to be set to the defaults or tests fail
if (class_exists(i18n::class)) {
i18n::set_locale(i18n::config()->uninherited('default_locale'));
}
// Set default timezone consistently to avoid NZ-specific dependencies
date_default_timezone_set('UTC');
if (class_exists(Member::class)) {
Member::set_password_validator(null);
}
if (class_exists(Cookie::class)) {
Cookie::config()->update('report_errors', false);
}
if (class_exists(RootURLController::class)) {
RootURLController::reset();
}
if (class_exists(Security::class)) {
Security::clear_database_is_ready();
}
// Set up test routes
$this->setUpRoutes();
$fixtureFiles = $this->getFixturePaths();
if ($this->shouldSetupDatabaseForCurrentTest($fixtureFiles)) {
// Assign fixture factory to deprecated prop in case old tests use it over the getter
/** @var FixtureTestState $fixtureState */
$fixtureState = static::$state->getStateByName('fixtures');
$this->fixtureFactory = $fixtureState->getFixtureFactory(static::class);
$this->logInWithPermission('ADMIN');
}
// turn off template debugging
if (class_exists(SSViewer::class)) {
SSViewer::config()->update('source_file_comments', false);
}
// Set up the test mailer
if (class_exists(TestMailer::class)) {
Injector::inst()->registerService(new TestMailer(), Mailer::class);
}
if (class_exists(Email::class)) {
Email::config()->remove('send_all_emails_to');
Email::config()->remove('send_all_emails_from');
Email::config()->remove('cc_all_emails_to');
Email::config()->remove('bcc_all_emails_to');
}
}
/**
* Helper method to determine if the current test should enable a test database
*
* @param $fixtureFiles
* @return bool
*/
protected function shouldSetupDatabaseForCurrentTest($fixtureFiles)
{
$databaseEnabledByDefault = $fixtureFiles || $this->usesDatabase;
return ($databaseEnabledByDefault && !$this->currentTestDisablesDatabase())
|| $this->currentTestEnablesDatabase();
}
/**
* Helper method to check, if the current test uses the database.
* This can be switched on with the annotation "@useDatabase"
*
* @return bool
*/
protected function currentTestEnablesDatabase()
{
$annotations = $this->getAnnotations();
return array_key_exists('useDatabase', $annotations['method'])
&& $annotations['method']['useDatabase'][0] !== 'false';
}
/**
* Helper method to check, if the current test uses the database.
* This can be switched on with the annotation "@useDatabase false"
*
* @return bool
*/
protected function currentTestDisablesDatabase()
{
$annotations = $this->getAnnotations();
return array_key_exists('useDatabase', $annotations['method'])
&& $annotations['method']['useDatabase'][0] === 'false';
}
/**
* Called once per test case ({@link SapphireTest} subclass).
* This is different to {@link setUp()}, which gets called once
* per method. Useful to initialize expensive operations which
* don't change state for any called method inside the test,
* e.g. dynamically adding an extension. See {@link teardownAfterClass()}
* for tearing down the state again.
*
* Always sets up in order:
* - Reset php state
* - Nest
* - Custom state helpers
*
* User code should call parent::setUpBeforeClass() before custom setup code
*
* @throws Exception
*/
public static function setUpBeforeClass(): void
{
// Start tests
static::start();
if (!static::$state) {
throw new Exception('SapphireTest failed to bootstrap!');
}
// Call state helpers
static::$state->setUpOnce(static::class);
// Build DB if we have objects
if (class_exists(DataObject::class) && static::getExtraDataObjects()) {
DataObject::reset();
static::resetDBSchema(true, true);
}
}
/**
* tearDown method that's called once per test class rather once per test method.
*
* Always sets up in order:
* - Custom state helpers
* - Unnest
* - Reset php state
*
* User code should call parent::tearDownAfterClass() after custom tear down code
*/
public static function tearDownAfterClass(): void
{
// Call state helpers
static::$state->tearDownOnce(static::class);
// Reset DB schema
static::resetDBSchema();
}
/**
* @return FixtureFactory|false
* @deprecated 4.0.0:5.0.0
*/
public function getFixtureFactory()
{
Deprecation::notice('5.0', __FUNCTION__ . ' is deprecated, use ' . FixtureTestState::class . ' instead');
/** @var FixtureTestState $state */
$state = static::$state->getStateByName('fixtures');
return $state->getFixtureFactory(static::class);
}
/**
* Sets a new fixture factory
* @param FixtureFactory $factory
* @return $this
* @deprecated 4.0.0:5.0.0
*/
public function setFixtureFactory(FixtureFactory $factory)
{
Deprecation::notice('5.0', __FUNCTION__ . ' is deprecated, use ' . FixtureTestState::class . ' instead');
/** @var FixtureTestState $state */
$state = static::$state->getStateByName('fixtures');
$state->setFixtureFactory($factory, static::class);
$this->fixtureFactory = $factory;
return $this;
}
/**
* Get the ID of an object from the fixture.
*
* @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work
* @param string $identifier The identifier string, as provided in your fixture file
* @return int
*/
protected function idFromFixture($className, $identifier)
{
/** @var FixtureTestState $state */
$state = static::$state->getStateByName('fixtures');
$id = $state->getFixtureFactory(static::class)->getId($className, $identifier);
if (!$id) {
throw new InvalidArgumentException(sprintf(
"Couldn't find object '%s' (class: %s)",
$identifier,
$className
));
}
return $id;
}
/**
* Return all of the IDs in the fixture of a particular class name.
* Will collate all IDs form all fixtures if multiple fixtures are provided.
*
* @param string $className The data class or table name, as specified in your fixture file
* @return array A map of fixture-identifier => object-id
*/
protected function allFixtureIDs($className)
{
/** @var FixtureTestState $state */
$state = static::$state->getStateByName('fixtures');
return $state->getFixtureFactory(static::class)->getIds($className);
}
/**
* Get an object from the fixture.
*
* @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work
* @param string $identifier The identifier string, as provided in your fixture file
*
* @return DataObject
*/
protected function objFromFixture($className, $identifier)
{
/** @var FixtureTestState $state */
$state = static::$state->getStateByName('fixtures');
$obj = $state->getFixtureFactory(static::class)->get($className, $identifier);
if (!$obj) {
throw new InvalidArgumentException(sprintf(
"Couldn't find object '%s' (class: %s)",
$identifier,
$className
));
}
return $obj;
}
/**
* Load a YAML fixture file into the database.
* Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture.
* Doesn't clear existing fixtures.
* @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir
* @deprecated 4.0.0:5.0.0
*
*/
public function loadFixture($fixtureFile)
{
Deprecation::notice('5.0', __FUNCTION__ . ' is deprecated, use ' . FixtureTestState::class . ' instead');
$fixture = Injector::inst()->create(YamlFixture::class, $fixtureFile);
$fixture->writeInto($this->getFixtureFactory());
}
/**
* Clear all fixtures which were previously loaded through
* {@link loadFixture()}
*/
public function clearFixtures()
{
/** @var FixtureTestState $state */
$state = static::$state->getStateByName('fixtures');
$state->getFixtureFactory(static::class)->clear();
}
/**
* Useful for writing unit tests without hardcoding folder structures.
*
* @return string Absolute path to current class.
*/
protected function getCurrentAbsolutePath()
{
$filename = ClassLoader::inst()->getItemPath(static::class);
if (!$filename) {
throw new LogicException('getItemPath returned null for ' . static::class
. '. Try adding flush=1 to the test run.');
}
return dirname($filename);
}
/**
* @return string File path relative to webroot
*/
protected function getCurrentRelativePath()
{
$base = Director::baseFolder();
$path = $this->getCurrentAbsolutePath();
if (substr($path, 0, strlen($base)) == $base) {
$path = preg_replace('/^\/*/', '', substr($path, strlen($base)));
}
return $path;
}
/**
* Setup the test.
* Always sets up in order:
* - Custom state helpers
* - Unnest
* - Reset php state
*
* User code should call parent::tearDown() after custom tear down code
*/
protected function tearDown(): void
{
// Reset mocked datetime
if (class_exists(DBDatetime::class)) {
DBDatetime::clear_mock_now();
}
// Stop the redirection that might have been requested in the test.
// Note: Ideally a clean Controller should be created for each test.
// Now all tests executed in a batch share the same controller.
if (class_exists(Controller::class)) {
$controller = Controller::has_curr() ? Controller::curr() : null;
if ($controller && ($response = $controller->getResponse()) && $response->getHeader('Location')) {
$response->setStatusCode(200);
$response->removeHeader('Location');
}
}
// Call state helpers
static::$state->tearDown($this);
}
/**
* Clear the log of emails sent
*
* @return bool True if emails cleared
*/
public function clearEmails()
{
/** @var Mailer $mailer */
$mailer = Injector::inst()->get(Mailer::class);
if ($mailer instanceof TestMailer) {
$mailer->clearEmails();
return true;
}
return false;
}
/**
* Search for an email that was sent.
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
* @param string $to
* @param string $from
* @param string $subject
* @param string $content
* @return array|null Contains keys: 'Type', 'To', 'From', 'Subject', 'Content', 'PlainContent', 'AttachedFiles',
* 'HtmlContent'
*/
public static function findEmail($to, $from = null, $subject = null, $content = null)
{
/** @var Mailer $mailer */
$mailer = Injector::inst()->get(Mailer::class);
if ($mailer instanceof TestMailer) {
return $mailer->findEmail($to, $from, $subject, $content);
}
return null;
}
/**
* Assert that the matching email was sent since the last call to clearEmails()
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
*
* @param string $to
* @param string $from
* @param string $subject
* @param string $content
*/
public static function assertEmailSent($to, $from = null, $subject = null, $content = null)
{
$found = (bool)static::findEmail($to, $from, $subject, $content);
$infoParts = '';
$withParts = [];
if ($to) {
$infoParts .= " to '$to'";
}
if ($from) {
$infoParts .= " from '$from'";
}
if ($subject) {
$withParts[] = "subject '$subject'";
}
if ($content) {
$withParts[] = "content '$content'";
}
if ($withParts) {
$infoParts .= ' with ' . implode(' and ', $withParts);
}
static::assertTrue(
$found,
"Failed asserting that an email was sent$infoParts."
);
}
/**
* Assert that the given {@link SS_List} includes DataObjects matching the given key-value
* pairs. Each match must correspond to 1 distinct record.
*
* @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can
* either pass a single pattern or an array of patterns.
* @param SS_List $list The {@link SS_List} to test.
* @param string $message
*
* Examples
* --------
* Check that $members includes an entry with Email = sam@example.com:
* $this->assertListContains(['Email' => '...@example.com'], $members);
*
* Check that $members includes entries with Email = sam@example.com and with
* Email = ingo@example.com:
* $this->assertListContains([
* ['Email' => '...@example.com'],
* ['Email' => 'i...@example.com'],
* ], $members);
*/
public static function assertListContains($matches, SS_List $list, $message = '')
{
if (!is_array($matches)) {
throw self::createInvalidArgumentException(
1,
'array'
);
}
static::assertThat(
$list,
new SSListContains(
$matches
),
$message
);
}
/**
* @param $matches
* @param $dataObjectSet
* @deprecated 4.0.0:5.0.0 Use assertListContains() instead
*
*/
public function assertDOSContains($matches, $dataObjectSet)
{
Deprecation::notice('5.0', 'Use assertListContains() instead');
static::assertListContains($matches, $dataObjectSet);
}
/**
* Asserts that no items in a given list appear in the given dataobject list
*
* @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can
* either pass a single pattern or an array of patterns.
* @param SS_List $list The {@link SS_List} to test.
* @param string $message
*
* Examples
* --------
* Check that $members doesn't have an entry with Email = sam@example.com:
* $this->assertListNotContains(['Email' => '...@example.com'], $members);
*
* Check that $members doesn't have entries with Email = sam@example.com and with
* Email = ingo@example.com:
* $this->assertListNotContains([
* ['Email' => '...@example.com'],
* ['Email' => 'i...@example.com'],
* ], $members);
*/
public static function assertListNotContains($matches, SS_List $list, $message = '')
{
if (!is_array($matches)) {
throw self::createInvalidArgumentException(
1,
'array'
);
}
$constraint = new LogicalNot(
new SSListContains(
$matches
)
);
static::assertThat(
$list,
$constraint,
$message
);
}
/**
* @param $matches
* @param $dataObjectSet
* @deprecated 4.0.0:5.0.0 Use assertListNotContains() instead
*
*/
public static function assertNotDOSContains($matches, $dataObjectSet)
{
Deprecation::notice('5.0', 'Use assertListNotContains() instead');
static::assertListNotContains($matches, $dataObjectSet);
}
/**
* Assert that the given {@link SS_List} includes only DataObjects matching the given
* key-value pairs. Each match must correspond to 1 distinct record.
*
* Example
* --------
* Check that *only* the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't
* matter:
* $this->assertListEquals([
* ['FirstName' =>'Sam', 'Surname' => 'Minnee'],
* ['FirstName' => 'Ingo', 'Surname' => 'Schommer'],
* ], $members);
*
* @param mixed $matches The patterns to match. Each pattern is a map of key-value pairs. You can
* either pass a single pattern or an array of patterns.
* @param mixed $list The {@link SS_List} to test.
* @param string $message
*/
public static function assertListEquals($matches, SS_List $list, $message = '')
{
if (!is_array($matches)) {
throw self::createInvalidArgumentException(
1,
'array'
);
}
static::assertThat(
$list,
new SSListContainsOnly(
$matches
),
$message
);
}
/**
* @param $matches
* @param SS_List $dataObjectSet
* @deprecated 4.0.0:5.0.0 Use assertListEquals() instead
*
*/
public function assertDOSEquals($matches, $dataObjectSet)
{
Deprecation::notice('5.0', 'Use assertListEquals() instead');
static::assertListEquals($matches, $dataObjectSet);
}
/**
* Assert that the every record in the given {@link SS_List} matches the given key-value
* pairs.
*
* Example
* --------
* Check that every entry in $members has a Status of 'Active':
* $this->assertListAllMatch(['Status' => 'Active'], $members);
*
* @param mixed $match The pattern to match. The pattern is a map of key-value pairs.
* @param mixed $list The {@link SS_List} to test.
* @param string $message
*/
public static function assertListAllMatch($match, SS_List $list, $message = '')
{
if (!is_array($match)) {
throw self::createInvalidArgumentException(
1,
'array'
);
}
static::assertThat(
$list,
new SSListContainsOnlyMatchingItems(
$match
),
$message
);
}
/**
* @param $match
* @param SS_List $dataObjectSet
* @deprecated 4.0.0:5.0.0 Use assertListAllMatch() instead
*
*/
public function assertDOSAllMatch($match, SS_List $dataObjectSet)
{
Deprecation::notice('5.0', 'Use assertListAllMatch() instead');
static::assertListAllMatch($match, $dataObjectSet);
}
/**
* Removes sequences of repeated whitespace characters from SQL queries
* making them suitable for string comparison
*
* @param string $sql
* @return string The cleaned and normalised SQL string
*/
protected static function normaliseSQL($sql)
{
return trim(preg_replace('/\s+/m', ' ', $sql));
}
/**
* Asserts that two SQL queries are equivalent
*
* @param string $expectedSQL
* @param string $actualSQL
* @param string $message
*/
public static function assertSQLEquals(
$expectedSQL,
$actualSQL,
$message = ''
) {
// Normalise SQL queries to remove patterns of repeating whitespace
$expectedSQL = static::normaliseSQL($expectedSQL);
$actualSQL = static::normaliseSQL($actualSQL);
static::assertEquals($expectedSQL, $actualSQL, $message);
}
/**
* Asserts that a SQL query contains a SQL fragment
*
* @param string $needleSQL
* @param string $haystackSQL
* @param string $message
*/
public static function assertSQLContains(
$needleSQL,
$haystackSQL,
$message = ''
) {
$needleSQL = static::normaliseSQL($needleSQL);
$haystackSQL = static::normaliseSQL($haystackSQL);
if (is_iterable($haystackSQL)) {
/** @var iterable $iterableHaystackSQL */
$iterableHaystackSQL = $haystackSQL;
static::assertContains($needleSQL, $iterableHaystackSQL, $message);
} else {
static::assertStringContainsString($needleSQL, $haystackSQL, $message);
}
}
/**
* Asserts that a SQL query contains a SQL fragment
*
* @param string $needleSQL
* @param string $haystackSQL
* @param string $message
*/
public static function assertSQLNotContains(
$needleSQL,
$haystackSQL,
$message = ''
) {
$needleSQL = static::normaliseSQL($needleSQL);
$haystackSQL = static::normaliseSQL($haystackSQL);
if (is_iterable($haystackSQL)) {
/** @var iterable $iterableHaystackSQL */
$iterableHaystackSQL = $haystackSQL;
static::assertNotContains($needleSQL, $iterableHaystackSQL, $message);
} else {
static::assertStringNotContainsString($needleSQL, $haystackSQL, $message);
}
}
/**
* Start test environment
*/
public static function start()
{
if (static::is_running_test()) {
return;
}
// Health check
if (InjectorLoader::inst()->countManifests()) {
throw new LogicException('SapphireTest::start() cannot be called within another application');
}
static::set_is_running_test(true);
// Test application
$kernel = new TestKernel(BASE_PATH);
if (class_exists(HTTPApplication::class)) {
// Mock request
$_SERVER['argv'] = ['vendor/bin/phpunit', '/'];
$request = CLIRequestBuilder::createFromEnvironment();
$app = new HTTPApplication($kernel);
$flush = array_key_exists('flush', $request->getVars());
// Custom application
$res = $app->execute($request, function (HTTPRequest $request) {
// Start session and execute
$request->getSession()->init($request);
// Invalidate classname spec since the test manifest will now pull out new subclasses for each internal class
// (e.g. Member will now have various subclasses of DataObjects that implement TestOnly)
DataObject::reset();
// Set dummy controller;
$controller = Controller::create();
$controller->setRequest($request);
$controller->pushCurrent();
$controller->doInit();
}, $flush);
if ($res && $res->isError()) {
throw new LogicException($res->getBody());
}
} else {
// Allow flush from the command line in the absence of HTTPApplication's special sauce
$flush = false;
foreach ($_SERVER['argv'] as $arg) {
if (preg_match('/^(--)?flush(=1)?$/', $arg)) {
$flush = true;
}
}
$kernel->boot($flush);
}
// Register state
static::$state = SapphireTestState::singleton();
// Register temp DB holder
static::tempDB();
}
/**
* Reset the testing database's schema, but only if it is active
* @param bool $includeExtraDataObjects If true, the extraDataObjects tables will also be included
* @param bool $forceCreate Force DB to be created if it doesn't exist
*/
public static function resetDBSchema($includeExtraDataObjects = false, $forceCreate = false)
{
if (!static::$tempDB) {
return;
}
// Check if DB is active before reset
if (!static::$tempDB->isUsed()) {
if (!$forceCreate) {
return;
}
static::$tempDB->build();
}
$extraDataObjects = $includeExtraDataObjects ? static::getExtraDataObjects() : [];
static::$tempDB->resetDBSchema((array)$extraDataObjects);
}
/**
* A wrapper for automatically performing callbacks as a user with a specific permission
*
* @param string|array $permCode
* @param callable $callback
* @return mixed
*/
public function actWithPermission($permCode, $callback)
{
return Member::actAs($this->createMemberWithPermission($permCode), $callback);
}
/**
* Create Member and Group objects on demand with specific permission code
*
* @param string|array $permCode
* @return Member
*/
protected function createMemberWithPermission($permCode)
{
if (is_array($permCode)) {
$permArray = $permCode;
$permCode = implode('.', $permCode);
} else {
$permArray = [$permCode];
}
// Check cached member
if (isset($this->cache_generatedMembers[$permCode])) {
$member = $this->cache_generatedMembers[$permCode];
} else {
// Generate group with these permissions
$group = Group::create();
$group->Title = "$permCode group";
$group->write();
// Create each individual permission
foreach ($permArray as $permArrayItem) {
$permission = Permission::create();
$permission->Code = $permArrayItem;
$permission->write();
$group->Permissions()->add($permission);
}
$member = Member::get()->filter([
'Email' => "$permCode@example.org",
])->first();
if (!$member) {
$member = Member::create();
}
$member->FirstName = $permCode;
$member->Surname = 'User';
$member->Email = "$permCode@example.org";
$member->write();
$group->Members()->add($member);
$this->cache_generatedMembers[$permCode] = $member;
}
return $member;
}
/**
* Create a member and group with the given permission code, and log in with it.
* Returns the member ID.
*
* @param string|array $permCode Either a permission, or list of permissions
* @return int Member ID
*/
public function logInWithPermission($permCode = 'ADMIN')
{
$member = $this->createMemberWithPermission($permCode);
$this->logInAs($member);
return $member->ID;
}
/**
* Log in as the given member
*
* @param Member|int|string $member The ID, fixture codename, or Member object of the member that you want to log in
*/
public function logInAs($member)
{
if (is_numeric($member)) {
$member = DataObject::get_by_id(Member::class, $member);
} elseif (!is_object($member)) {
$member = $this->objFromFixture(Member::class, $member);
}
Injector::inst()->get(IdentityStore::class)->logIn($member);
}
/**
* Log out the current user
*/
public function logOut()
{
/** @var IdentityStore $store */
$store = Injector::inst()->get(IdentityStore::class);
$store->logOut();
}
/**
* Cache for logInWithPermission()
*/
protected $cache_generatedMembers = [];
/**
* Test against a theme.
*
* @param string $themeBaseDir themes directory
* @param string $theme Theme name
* @param callable $callback
* @throws Exception
*/
protected function useTestTheme($themeBaseDir, $theme, $callback)
{
Config::nest();
if (strpos($themeBaseDir, BASE_PATH) === 0) {
$themeBaseDir = substr($themeBaseDir, strlen(BASE_PATH));
}
SSViewer::config()->update('theme_enabled', true);
SSViewer::set_themes([$themeBaseDir . '/themes/' . $theme, '$default']);
try {
$callback();
} finally {
Config::unnest();
}
}
/**
* Get fixture paths for this test
*
* @return array List of paths
*/
protected function getFixturePaths()
{
$fixtureFile = static::get_fixture_file();
if (empty($fixtureFile)) {
return [];
}
$fixtureFiles = is_array($fixtureFile) ? $fixtureFile : [$fixtureFile];
return array_map(function ($fixtureFilePath) {
return $this->resolveFixturePath($fixtureFilePath);
}, $fixtureFiles);
}
/**
* Return all extra objects to scaffold for this test
* @return array
*/
public static function getExtraDataObjects()
{
return static::$extra_dataobjects;
}
/**
* Get additional controller classes to register routes for
*
* @return array
*/
public static function getExtraControllers()
{
return static::$extra_controllers;
}
/**
* Map a fixture path to a physical file
*
* @param string $fixtureFilePath
* @return string
*/
protected function resolveFixturePath($fixtureFilePath)
{
// support loading via composer name path.
if (strpos($fixtureFilePath, ':') !== false) {
return ModuleResourceLoader::singleton()->resolvePath($fixtureFilePath);
}
// Support fixture paths relative to the test class, rather than relative to webroot
// String checking is faster than file_exists() calls.
$resolvedPath = realpath($this->getCurrentAbsolutePath() . '/' . $fixtureFilePath);
if ($resolvedPath) {
return $resolvedPath;
}
// Check if file exists relative to base dir
$resolvedPath = realpath(Director::baseFolder() . '/' . $fixtureFilePath);
if ($resolvedPath) {
return $resolvedPath;
}
return $fixtureFilePath;
}
protected function setUpRoutes()
{
if (!class_exists(Director::class)) {
return;
}
// Get overridden routes
$rules = $this->getExtraRoutes();
// Add all other routes
foreach (Director::config()->uninherited('rules') as $route => $rule) {
if (!isset($rules[$route])) {
$rules[$route] = $rule;
}
}
// Add default catch-all rule
$rules['$Controller//$Action/$ID/$OtherID'] = '*';
// Add controller-name auto-routing
Director::config()->set('rules', $rules);
}
/**
* Get extra routes to merge into Director.rules
*
* @return array
*/
protected function getExtraRoutes()
{
$rules = [];
foreach ($this->getExtraControllers() as $class) {
$controllerInst = Controller::singleton($class);
$link = Director::makeRelative($controllerInst->Link());
$route = rtrim($link, '/') . '//$Action/$ID/$OtherID';
$rules[$route] = $class;
}
return $rules;
}
/**
* Reimplementation of phpunit5 PHPUnit_Util_InvalidArgumentHelper::factory()
*
* @param $argument
* @param $type
* @param $value
*/
public static function createInvalidArgumentException($argument, $type, $value = null)
{
$stack = debug_backtrace(false);
return new PHPUnitFrameworkException(
sprintf(
'Argument #%d%sof %s::%s() must be a %s',
$argument,
$value !== null ? ' (' . gettype($value) . '#' . $value . ')' : ' (No Value) ',
$stack[1]['class'],
$stack[1]['function'],
$type
)
);
}
/**
* Returns the annotations for this test.
*
* @return array
*/
public function getAnnotations(): array
{
return TestUtil::parseTestMethodAnnotations(
get_class($this),
$this->getName(false)
);
}
}
}
/* -------------------------------------------------
*
* This version of SapphireTest is for phpunit 5
* The phpunit 9 verison is at the top of this file
*
* PHPUnit_Extensions_GroupTestSuite is a class that only exists in phpunit 5
*
* -------------------------------------------------
*/
if (!class_exists(PHPUnit_Extensions_GroupTestSuite::class)) {
return;
}
@ -51,8 +1372,12 @@ if (!class_exists(PHPUnit_Framework_TestCase::class)) {
* This class should not be used anywhere outside of unit tests, as phpunit may not be installed
* in production sites.
*/
// Ignore multiple classes in same file
// @codingStandardsIgnoreStart
class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
{
// @codingStandardsIgnoreEnd
/**
* Path to fixture data for this test run.
* If passed as an array, multiple fixture files will be loaded.
@ -340,7 +1665,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
}
/**
* Helper method to determine if the current test should enable a test database
*
@ -439,8 +1763,8 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
}
/**
* @deprecated 4.0.0:5.0.0
* @return FixtureFactory|false
* @deprecated 4.0.0:5.0.0
*/
public function getFixtureFactory()
{
@ -452,9 +1776,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
/**
* Sets a new fixture factory
* @deprecated 4.0.0:5.0.0
* @param FixtureFactory $factory
* @return $this
* @deprecated 4.0.0:5.0.0
*/
public function setFixtureFactory(FixtureFactory $factory)
{
@ -533,9 +1857,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
* Load a YAML fixture file into the database.
* Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture.
* Doesn't clear existing fixtures.
* @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir
* @deprecated 4.0.0:5.0.0
*
* @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir
*/
public function loadFixture($fixtureFile)
{
@ -715,7 +2039,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
);
}
/**
* Assert that the given {@link SS_List} includes DataObjects matching the given key-value
* pairs. Each match must correspond to 1 distinct record.
@ -756,10 +2079,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
}
/**
* @deprecated 4.0.0:5.0.0 Use assertListContains() instead
*
* @param $matches
* @param $dataObjectSet
* @deprecated 4.0.0:5.0.0 Use assertListContains() instead
*
*/
public function assertDOSContains($matches, $dataObjectSet)
{
@ -796,7 +2119,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
);
}
$constraint = new PHPUnit_Framework_Constraint_Not(
$constraint = new PHPUnit_Framework_Constraint_Not(
new SSListContains(
$matches
)
@ -810,10 +2133,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
}
/**
* @deprecated 4.0.0:5.0.0 Use assertListNotContains() instead
*
* @param $matches
* @param $dataObjectSet
* @deprecated 4.0.0:5.0.0 Use assertListNotContains() instead
*
*/
public static function assertNotDOSContains($matches, $dataObjectSet)
{
@ -858,10 +2181,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
}
/**
* @deprecated 4.0.0:5.0.0 Use assertListEquals() instead
*
* @param $matches
* @param SS_List $dataObjectSet
* @deprecated 4.0.0:5.0.0 Use assertListEquals() instead
*
*/
public function assertDOSEquals($matches, $dataObjectSet)
{
@ -869,7 +2192,6 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
return static::assertListEquals($matches, $dataObjectSet);
}
/**
* Assert that the every record in the given {@link SS_List} matches the given key-value
* pairs.
@ -902,10 +2224,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
}
/**
* @deprecated 4.0.0:5.0.0 Use assertListAllMatch() instead
*
* @param $match
* @param SS_List $dataObjectSet
* @deprecated 4.0.0:5.0.0 Use assertListAllMatch() instead
*
*/
public function assertDOSAllMatch($match, SS_List $dataObjectSet)
{

View File

@ -7,6 +7,7 @@ use Behat\Behat\Context\Context;
use Behat\Mink\Exception\ElementHtmlException;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Session;
use PHPUnit\Framework\Assert;
use SilverStripe\BehatExtension\Context\MainContextAwareTrait;
use SilverStripe\BehatExtension\Utility\StepHelper;
use Symfony\Component\DomCrawler\Crawler;
@ -55,9 +56,9 @@ class CmsFormsContext implements Context
$form = $page->find('css', '#Form_EditForm');
if (trim($negative)) {
assertNull($form, 'I should not see an edit page form');
Assert::assertNull($form, 'I should not see an edit page form');
} else {
assertNotNull($form, 'I should see an edit page form');
Assert::assertNotNull($form, 'I should see an edit page form');
}
}
@ -154,20 +155,31 @@ class CmsFormsContext implements Context
$matchedNode = $node;
}
}
assertNotNull($matchedNode);
Assert::assertNotNull($matchedNode);
$assertFn = $negate ? 'assertNotEquals' : 'assertEquals';
if($formatting == 'bold') {
call_user_func($assertFn, 'strong', $matchedNode->nodeName);
} else if($formatting == 'left aligned') {
if($matchedNode->getAttribute('class')) {
call_user_func($assertFn, 'text-left', $matchedNode->getAttribute('class'));
}
} else if($formatting == 'right aligned') {
call_user_func($assertFn, 'text-right', $matchedNode->getAttribute('class'));
}
}
// @codingStandardsIgnoreEnd
if ($formatting == 'bold') {
if ($negate) {
Assert::assertNotEquals('strong', $matchedNode->nodeName);
} else {
Assert::assertEquals('strong', $matchedNode->nodeName);
}
} else if ($formatting == 'left aligned') {
if ($matchedNode->getAttribute('class')) {
if ($negate) {
Assert::assertNotEquals('text-left', $matchedNode->getAttribute('class'));
} else {
Assert::assertEquals('text-left', $matchedNode->getAttribute('class'));
}
}
} else if ($formatting == 'right aligned') {
if ($negate) {
Assert::assertNotEquals('text-right', $matchedNode->getAttribute('class'));
} else {
Assert::assertEquals('text-right', $matchedNode->getAttribute('class'));
}
}
}
// @codingStandardsIgnoreEnd
/**
* Selects the first textual match in the HTML editor. Does not support
@ -226,9 +238,9 @@ JS;
}
if (trim($negative)) {
assertNull($matchedEl);
Assert::assertNull($matchedEl);
} else {
assertNotNull($matchedEl);
Assert::assertNotNull($matchedEl);
}
}
@ -287,19 +299,19 @@ JS;
$parentElement = null;
$this->retryThrowable(function () use (&$parentElement, &$page, $selector) {
$parentElement = $page->find('css', $selector);
assertNotNull($parentElement, sprintf('"%s" element not found', $selector));
Assert::assertNotNull($parentElement, sprintf('"%s" element not found', $selector));
$page = $this->getSession()->getPage();
});
$this->retryThrowable(function () use ($parentElement, $selector) {
$dropdown = $parentElement->find('css', '.Select-arrow');
assertNotNull($dropdown, sprintf('Unable to find the dropdown in "%s"', $selector));
Assert::assertNotNull($dropdown, sprintf('Unable to find the dropdown in "%s"', $selector));
$dropdown->click();
});
$this->retryThrowable(function () use ($text, $parentElement, $selector) {
$element = $parentElement->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $text));
assertNotNull($element, sprintf('"%s" not found in "%s"', $text, $selector));
Assert::assertNotNull($element, sprintf('"%s" not found in "%s"', $text, $selector));
$element->click();
});
}
@ -322,7 +334,7 @@ JS;
$element = $this->findInputByLabelContent($locator);
}
assertNotNull($element, sprintf('HTML field "%s" not found', $locator));
Assert::assertNotNull($element, sprintf('HTML field "%s" not found', $locator));
return $element;
}
@ -335,7 +347,7 @@ JS;
return null;
}
assertCount(1, $label, sprintf(
Assert::assertCount(1, $label, sprintf(
'Found more than one element containing the phrase "%s".',
$locator
));
@ -406,7 +418,7 @@ JS;
public function assertIShouldSeeTheGridFieldButtonForRow($buttonLabel, $gridFieldName, $rowName)
{
$button = $this->getGridFieldButton($gridFieldName, $rowName, $buttonLabel);
assertNotNull($button, sprintf('Button "%s" not found', $buttonLabel));
Assert::assertNotNull($button, sprintf('Button "%s" not found', $buttonLabel));
}
/**
@ -418,7 +430,7 @@ JS;
public function assertIShouldNotSeeTheGridFieldButtonForRow($buttonLabel, $gridFieldName, $rowName)
{
$button = $this->getGridFieldButton($gridFieldName, $rowName, $buttonLabel);
assertNull($button, sprintf('Button "%s" found', $buttonLabel));
Assert::assertNull($button, sprintf('Button "%s" found', $buttonLabel));
}
/**
@ -430,7 +442,7 @@ JS;
public function stepIClickTheGridFieldButtonForRow($buttonLabel, $gridFieldName, $rowName)
{
$button = $this->getGridFieldButton($gridFieldName, $rowName, $buttonLabel);
assertNotNull($button, sprintf('Button "%s" not found', $buttonLabel));
Assert::assertNotNull($button, sprintf('Button "%s" not found', $buttonLabel));
$button->click();
}
@ -447,7 +459,7 @@ JS;
{
$page = $this->getSession()->getPage();
$gridField = $page->find('xpath', sprintf('//*[@data-name="%s"]', $gridFieldName));
assertNotNull($gridField, sprintf('Gridfield "%s" not found', $gridFieldName));
Assert::assertNotNull($gridField, sprintf('Gridfield "%s" not found', $gridFieldName));
$name = $gridField->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $rowName));
if (!$name) {
@ -472,12 +484,12 @@ JS;
{
$page = $this->getSession()->getPage();
$listBox = $page->find('xpath', sprintf('//*[@name="%s[]"]', $fieldName));
assertNotNull($listBox, sprintf('The listbox %s is not found', $fieldName));
Assert::assertNotNull($listBox, sprintf('The listbox %s is not found', $fieldName));
$option = $listBox->getParent()
->find('css', '.chosen-choices')
->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $optionLabel));
assertNotNull($option, sprintf('Option %s is not found', $optionLabel));
Assert::assertNotNull($option, sprintf('Option %s is not found', $optionLabel));
$button = $option->getParent()->find('css', 'a');

View File

@ -8,6 +8,7 @@ use Behat\Mink\Element\Element;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Selector\Xpath\Escaper;
use Behat\Mink\Session;
use PHPUnit\Framework\Assert;
use SilverStripe\BehatExtension\Context\MainContextAwareTrait;
use SilverStripe\BehatExtension\Utility\StepHelper;
@ -72,7 +73,7 @@ class CmsUiContext implements Context
{
$page = $this->getSession()->getPage();
$cms_element = $page->find('css', '.cms');
assertNotNull($cms_element, 'CMS not found');
Assert::assertNotNull($cms_element, 'CMS not found');
}
/**
@ -116,9 +117,9 @@ class CmsUiContext implements Context
{
$page = $this->getMainContext()->getSession()->getPage();
$toasts = $page->find('css', '.toasts');
assertNotNull($toasts, "We have a toast container");
Assert::assertNotNull($toasts, "We have a toast container");
$toastAction = $toasts->find('named', ['link_or_button', "'{$action}'"]);
assertNotNull($toastAction, "We have a $action toast action");
Assert::assertNotNull($toastAction, "We have a $action toast action");
$toastAction->click();
}
@ -145,7 +146,7 @@ class CmsUiContext implements Context
$page = $this->getSession()->getPage();
$cms_content_header_tabs = $page->find('css', '.cms-content-header-tabs');
assertNotNull($cms_content_header_tabs, 'CMS tabs not found');
Assert::assertNotNull($cms_content_header_tabs, 'CMS tabs not found');
return $cms_content_header_tabs;
}
@ -160,7 +161,7 @@ class CmsUiContext implements Context
$page = $this->getSession()->getPage();
$cms_content_toolbar_element = $page->find('css', '.cms-content-toolbar');
assertNotNull($cms_content_toolbar_element, 'CMS content toolbar not found');
Assert::assertNotNull($cms_content_toolbar_element, 'CMS content toolbar not found');
return $cms_content_toolbar_element;
}
@ -174,7 +175,7 @@ class CmsUiContext implements Context
$page = $this->getSession()->getPage();
$cms_tree_element = $page->find('css', '.cms-tree');
assertNotNull($cms_tree_element, 'CMS tree not found');
Assert::assertNotNull($cms_tree_element, 'CMS tree not found');
return $cms_tree_element;
}
@ -187,7 +188,7 @@ class CmsUiContext implements Context
$cms_content_toolbar_element = $this->getCmsContentToolbarElement();
$element = $cms_content_toolbar_element->find('named', ['link_or_button', "'$text'"]);
assertNotNull($element, sprintf('%s button not found', $text));
Assert::assertNotNull($element, sprintf('%s button not found', $text));
}
/**
@ -198,7 +199,7 @@ class CmsUiContext implements Context
// Wait until visible
$cmsTreeElement = $this->getCmsTreeElement();
$element = $cmsTreeElement->find('named', ['content', "'$text'"]);
assertNotNull($element, sprintf('%s not found', $text));
Assert::assertNotNull($element, sprintf('%s not found', $text));
}
/**
@ -209,7 +210,7 @@ class CmsUiContext implements Context
// Wait until not visible
$cmsTreeElement = $this->getCmsTreeElement();
$element = $cmsTreeElement->find('named', ['content', "'$text'"]);
assertNull($element, sprintf('%s found', $text));
Assert::assertNull($element, sprintf('%s found', $text));
}
/**
@ -224,14 +225,14 @@ class CmsUiContext implements Context
);
$page = $this->getSession()->getPage();
$cmsListElement = $page->find('css', '.cms-list');
assertNotNull($cmsListElement, 'CMS list not found');
Assert::assertNotNull($cmsListElement, 'CMS list not found');
// Check text within this element
$element = $cmsListElement->find('named', ['content', "'$text'"]);
if (strstr($negate, 'not')) {
assertNull($element, sprintf('Unexpected %s found in cms list', $text));
Assert::assertNull($element, sprintf('Unexpected %s found in cms list', $text));
} else {
assertNotNull($element, sprintf('Expected %s not found in cms list', $text));
Assert::assertNotNull($element, sprintf('Expected %s not found in cms list', $text));
}
}
@ -241,7 +242,7 @@ class CmsUiContext implements Context
public function stepIShouldSeeInCMSContentTabs($text)
{
// Wait until visible
assertNotNull($this->getCmsTabElement($text), sprintf('%s content tab not found', $text));
Assert::assertNotNull($this->getCmsTabElement($text), sprintf('%s content tab not found', $text));
}
/**
@ -283,7 +284,7 @@ class CmsUiContext implements Context
"window.jQuery && window.jQuery('.jstree-apple-context').size() > 0"
);
$regionObj = $context->getRegionObj('.jstree-apple-context');
assertNotNull($regionObj, "Context menu could not be found");
Assert::assertNotNull($regionObj, "Context menu could not be found");
$linkObj = $regionObj->findLink($link);
if (empty($linkObj)) {
@ -304,7 +305,7 @@ class CmsUiContext implements Context
{
$treeEl = $this->getCmsTreeElement();
$treeNode = $treeEl->findLink($text);
assertNotNull($treeNode, sprintf('%s not found', $text));
Assert::assertNotNull($treeNode, sprintf('%s not found', $text));
$this->interactWithElement($treeNode, $method);
}
@ -314,7 +315,7 @@ class CmsUiContext implements Context
public function stepIClickOnElementInTheHeaderTabs($method, $text)
{
$tabsNode = $this->getCmsTabElement($text);
assertNotNull($tabsNode, sprintf('%s not found', $text));
Assert::assertNotNull($tabsNode, sprintf('%s not found', $text));
$this->interactWithElement($tabsNode, $method);
}
@ -324,8 +325,8 @@ class CmsUiContext implements Context
public function theHeaderTabShouldBeActive($text)
{
$element = $this->getCmsTabElement($text);
assertNotNull($element);
assertTrue($element->hasClass('active'));
Assert::assertNotNull($element);
Assert::assertTrue($element->hasClass('active'));
}
/**
@ -334,8 +335,8 @@ class CmsUiContext implements Context
public function theHeaderTabShouldNotBeActive($text)
{
$element = $this->getCmsTabElement($text);
assertNotNull($element);
assertFalse($element->hasClass('active'));
Assert::assertNotNull($element);
Assert::assertFalse($element->hasClass('active'));
}
/**
@ -354,7 +355,7 @@ class CmsUiContext implements Context
//Tries to find the first visiable toggle in the page
$page = $this->getSession()->getPage();
$toggle_elements = $page->findAll('css', '.toggle-expand');
assertNotNull($toggle_elements, 'Panel toggle not found');
Assert::assertNotNull($toggle_elements, 'Panel toggle not found');
/** @var NodeElement $toggle */
foreach ($toggle_elements as $toggle) {
if ($toggle->isVisible()) {
@ -370,7 +371,7 @@ class CmsUiContext implements Context
{
$page = $this->getSession()->getPage();
$filterButton = $page->find('css', '.search-box__filter-trigger');
assertNotNull($filterButton, sprintf('Filter button link not found'));
Assert::assertNotNull($filterButton, sprintf('Filter button link not found'));
$filterButtonExpanded = $filterButton->getAttribute('aria-expanded');
@ -421,20 +422,20 @@ SCRIPT
//Tries to find the first visiable matched Node in the page
$treeEl = $this->getCmsTreeElement();
$treeNode = $treeEl->findLink($nodeText);
assertNotNull($treeNode, sprintf('%s link not found', $nodeText));
Assert::assertNotNull($treeNode, sprintf('%s link not found', $nodeText));
$cssIcon = $treeNode->getParent()->getAttribute("class");
if ($action == "expand") {
//ensure it is collapsed
if (false === strpos($cssIcon, 'jstree-open')) {
$nodeIcon = $treeNode->getParent()->find('css', '.jstree-icon');
assertTrue($nodeIcon->isVisible(), "CMS node '$nodeText' not found");
Assert::assertTrue($nodeIcon->isVisible(), "CMS node '$nodeText' not found");
$nodeIcon->click();
}
} else {
//ensure it is expanded
if (false === strpos($cssIcon, 'jstree-closed')) {
$nodeIcon = $treeNode->getParent()->find('css', '.jstree-icon');
assertTrue($nodeIcon->isVisible(), "CMS node '$nodeText' not found");
Assert::assertTrue($nodeIcon->isVisible(), "CMS node '$nodeText' not found");
$nodeIcon->click();
}
}
@ -452,7 +453,7 @@ SCRIPT
$page = $this->getSession()->getPage();
$tabsets = $page->findAll('css', '.ui-tabs-nav');
assertNotNull($tabsets, 'CMS tabs not found');
Assert::assertNotNull($tabsets, 'CMS tabs not found');
$tab_element = null;
/** @var NodeElement $tabset */
@ -463,9 +464,9 @@ SCRIPT
}
}
if ($negate) {
assertNull($tab_element, sprintf('%s tab found', $tab));
Assert::assertNull($tab_element, sprintf('%s tab found', $tab));
} else {
assertNotNull($tab_element, sprintf('%s tab not found', $tab));
Assert::assertNotNull($tab_element, sprintf('%s tab not found', $tab));
}
}
@ -481,7 +482,7 @@ SCRIPT
$page = $this->getSession()->getPage();
$tabsets = $page->findAll('css', '.ui-tabs-nav');
assertNotNull($tabsets, 'CMS tabs not found');
Assert::assertNotNull($tabsets, 'CMS tabs not found');
$tab_element = null;
/** @var NodeElement $tabset */
@ -491,7 +492,7 @@ SCRIPT
}
$tab_element = $tabset->find('named', ['link_or_button', "'$tab'"]);
}
assertNotNull($tab_element, sprintf('%s tab not found', $tab));
Assert::assertNotNull($tab_element, sprintf('%s tab not found', $tab));
$tab_element->click();
}
@ -544,13 +545,13 @@ SCRIPT
public function iSwitchThePreviewToMode($mode)
{
$controls = $this->getSession()->getPage()->find('css', '.cms-preview-controls');
assertNotNull($controls, 'Preview controls not found');
Assert::assertNotNull($controls, 'Preview controls not found');
$label = $controls->find('xpath', sprintf(
'.//*[count(*)=0 and contains(text(), \'%s\')]',
$mode
));
assertNotNull($label, 'Preview mode switch not found');
Assert::assertNotNull($label, 'Preview mode switch not found');
$label->click();
@ -654,7 +655,7 @@ SCRIPT
}
}
assertGreaterThan(0, count($formFields), sprintf(
Assert::assertGreaterThan(0, count($formFields), sprintf(
'Chosen.js dropdown named "%s" not found',
$field
));
@ -669,11 +670,11 @@ SCRIPT
}
}
assertNotNull($container, 'Chosen.js field container not found');
Assert::assertNotNull($container, 'Chosen.js field container not found');
// Click on newly expanded list element, indirectly setting the dropdown value
$linkEl = $container->find('xpath', './/a');
assertNotNull($linkEl, 'Chosen.js link element not found');
Assert::assertNotNull($linkEl, 'Chosen.js link element not found');
$this->getSession()->wait(100); // wait for dropdown overlay to appear
$linkEl->click();

View File

@ -5,6 +5,7 @@ namespace SilverStripe\Framework\Tests\Behaviour;
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use InvalidArgumentException;
use PHPUnit\Framework\Assert;
use SilverStripe\BehatExtension\Context\MainContextAwareTrait;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
@ -109,19 +110,19 @@ class ConfigContext implements Context
// Ensure site is in dev mode
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
assertEquals(Kernel::DEV, $kernel->getEnvironment(), "Site is in dev mode");
Assert::assertEquals(Kernel::DEV, $kernel->getEnvironment(), "Site is in dev mode");
// Ensure file exists in specified fixture dir
$sourceDir = $this->getConfigPath();
$sourcePath = "{$sourceDir}/{$filename}";
assertFileExists($sourcePath, "Config file {$filename} exists");
Assert::assertFileExists($sourcePath, "Config file {$filename} exists");
// Get destination
$project = ModuleManifest::config()->get('project') ?: 'mysite';
$mysite = ModuleLoader::getModule($project);
assertNotNull($mysite, 'Project exists');
Assert::assertNotNull($mysite, 'Project exists');
$destPath = $mysite->getResource("_config/{$filename}")->getPath();
assertFileNotExists($destPath, "Config file {$filename} hasn't aleady been loaded");
Assert::assertFileDoesNotExist($destPath, "Config file {$filename} hasn't aleady been loaded");
// Load
$this->activatedConfigFiles[] = $destPath;

View File

@ -42,7 +42,7 @@ class ControllerTest extends FunctionalTest
UnsecuredController::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Director::config()->update('alternate_base_url', '/');
@ -60,7 +60,7 @@ class ControllerTest extends FunctionalTest
{
/* For a controller with a template, the default action will simple run that template. */
$response = $this->get("TestController/");
$this->assertContains("This is the main template. Content is 'default content'", $response->getBody());
$this->assertStringContainsString("This is the main template. Content is 'default content'", $response->getBody());
}
public function testMethodActions()
@ -68,18 +68,18 @@ class ControllerTest extends FunctionalTest
/* The Action can refer to a method that is called on the object. If a method returns an array, then it
* will be used to customise the template data */
$response = $this->get("TestController/methodaction");
$this->assertContains("This is the main template. Content is 'methodaction content'.", $response->getBody());
$this->assertStringContainsString("This is the main template. Content is 'methodaction content'.", $response->getBody());
/* If the method just returns a string, then that will be used as the response */
$response = $this->get("TestController/stringaction");
$this->assertContains("stringaction was called.", $response->getBody());
$this->assertStringContainsString("stringaction was called.", $response->getBody());
}
public function testTemplateActions()
{
/* If there is no method, it can be used to point to an alternative template. */
$response = $this->get("TestController/templateaction");
$this->assertContains(
$this->assertStringContainsString(
"This is the template for templateaction. Content is 'default content'.",
$response->getBody()
);
@ -262,12 +262,10 @@ class ControllerTest extends FunctionalTest
$this->logOut();
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid allowed_action '*'
*/
public function testWildcardAllowedActions()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid allowed_action '*'");
$this->get('AccessWildcardSecuredController');
}

View File

@ -10,7 +10,7 @@ use SilverStripe\Control\Cookie;
class CookieTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Injector::inst()->registerService(new CookieJar($_COOKIE), 'SilverStripe\\Control\\Cookie_Backend');

View File

@ -29,7 +29,7 @@ class DirectorTest extends SapphireTest
private $originalEnvType;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Director::config()->set('alternate_base_url', 'http://www.mysite.com:9090/');
@ -44,10 +44,10 @@ class DirectorTest extends SapphireTest
$this->expectedRedirect = null;
}
protected function tearDown(...$args)
protected function tearDown(): void
{
Environment::setEnv('SS_ENVIRONMENT_TYPE', $this->originalEnvType);
parent::tearDown(...$args);
parent::tearDown();
}
protected function getExtraRoutes()

View File

@ -3,7 +3,7 @@
namespace SilverStripe\Control\Tests\Email;
use DateTime;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Email\Mailer;
use SilverStripe\Control\Email\SwiftMailer;
@ -136,7 +136,7 @@ class EmailTest extends SapphireTest
public function testSend()
{
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
/** @var Email|MockObject $email */
$email = $this->makeEmailMock('Test send HTML');
// email should not call render if a body is supplied
@ -166,7 +166,7 @@ class EmailTest extends SapphireTest
public function testRenderedSend()
{
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
/** @var Email|MockObject $email */
$email = $this->getMockBuilder(Email::class)
->enableProxyingToOriginalMethods()
->getMock();
@ -193,7 +193,7 @@ class EmailTest extends SapphireTest
'$default',
]);
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
/** @var Email|MockObject $email */
$email = $this->getMockBuilder(EmailSubClass::class)
->enableProxyingToOriginalMethods()
->getMock();
@ -207,7 +207,7 @@ class EmailTest extends SapphireTest
$email->send();
$this->assertTrue($email->hasPlainPart());
$this->assertNotEmpty($email->getBody());
$this->assertContains('<h1>Email Sub-class</h1>', $email->getBody());
$this->assertStringContainsString('<h1>Email Sub-class</h1>', $email->getBody());
}
public function testConsturctor()
@ -527,7 +527,7 @@ class EmailTest extends SapphireTest
public function testGetFailedRecipients()
{
$mailer = new SwiftMailer();
/** @var Swift_NullTransport|PHPUnit_Framework_MockObject_MockObject $transport */
/** @var Swift_NullTransport|MockObject $transport */
$transport = $this->getMockBuilder(Swift_NullTransport::class)->getMock();
$transport->expects($this->once())
->method('send')
@ -552,7 +552,7 @@ class EmailTest extends SapphireTest
'EmailContent' => 'my content',
]);
$email->render();
$this->assertContains('my content', $email->getBody());
$this->assertStringContainsString('my content', $email->getBody());
$children = $email->getSwiftMessage()->getChildren();
$this->assertCount(1, $children);
$plainPart = reset($children);
@ -570,7 +570,7 @@ class EmailTest extends SapphireTest
'EmailContent' => 'my content',
]);
$email->render();
$this->assertContains('my content', $email->getBody());
$this->assertStringContainsString('my content', $email->getBody());
$children = $email->getSwiftMessage()->getChildren();
$this->assertCount(1, $children);
$plainPart = reset($children);
@ -581,19 +581,19 @@ class EmailTest extends SapphireTest
'EmailContent' => 'your content'
]);
$email->render();
$this->assertContains('your content', $email->getBody());
$this->assertStringContainsString('your content', $email->getBody());
// Ensure removing data causes a rerender
$email->removeData('EmailContent');
$email->render();
$this->assertNotContains('your content', $email->getBody());
$this->assertStringNotContainsString('your content', $email->getBody());
// Ensure adding data causes a rerender
$email->addData([
'EmailContent' => 'their content'
]);
$email->render();
$this->assertContains('their content', $email->getBody());
$this->assertStringContainsString('their content', $email->getBody());
}
public function testRenderPlainOnly()
@ -631,8 +631,8 @@ class EmailTest extends SapphireTest
$children = $email->getSwiftMessage()->getChildren();
$this->assertCount(1, $children);
$plainPart = reset($children);
$this->assertContains('Test', $plainPart->getBody());
$this->assertNotContains('<h1>Test</h1>', $plainPart->getBody());
$this->assertStringContainsString('Test', $plainPart->getBody());
$this->assertStringNotContainsString('<h1>Test</h1>', $plainPart->getBody());
}
public function testMultipleEmailSends()
@ -644,30 +644,30 @@ class EmailTest extends SapphireTest
$this->assertEmpty($email->getBody());
$this->assertEmpty($email->getSwiftMessage()->getChildren());
$email->send();
$this->assertContains('Test', $email->getBody());
$this->assertStringContainsString('Test', $email->getBody());
$this->assertCount(1, $email->getSwiftMessage()->getChildren());
$children = $email->getSwiftMessage()->getChildren();
/** @var \Swift_MimePart $plainPart */
$plainPart = reset($children);
$this->assertContains('Test', $plainPart->getBody());
$this->assertStringContainsString('Test', $plainPart->getBody());
//send again
$email->send();
$this->assertContains('Test', $email->getBody());
$this->assertStringContainsString('Test', $email->getBody());
$this->assertCount(1, $email->getSwiftMessage()->getChildren());
$children = $email->getSwiftMessage()->getChildren();
/** @var \Swift_MimePart $plainPart */
$plainPart = reset($children);
$this->assertContains('Test', $plainPart->getBody());
$this->assertStringContainsString('Test', $plainPart->getBody());
}
/**
* @return PHPUnit_Framework_MockObject_MockObject|Email
* @return MockObject|Email
*/
protected function makeEmailMock($subject)
{
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
/** @var Email|MockObject $email */
$email = $this->getMockBuilder(Email::class)
->enableProxyingToOriginalMethods()
->getMock();

View File

@ -9,7 +9,7 @@ use SilverStripe\Dev\SapphireTest;
class SwiftPluginTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -16,7 +16,7 @@ class HTTPCacheControlIntegrationTest extends FunctionalTest
RuleController::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
HTTPCacheControlMiddleware::config()
@ -31,11 +31,11 @@ class HTTPCacheControlIntegrationTest extends FunctionalTest
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/showform');
$header = $response->getHeader('Cache-Control');
$this->assertFalse($response->isError());
$this->assertNotContains('public', $header);
$this->assertNotContains('private', $header);
$this->assertContains('no-cache', $header);
$this->assertContains('no-store', $header);
$this->assertContains('must-revalidate', $header);
$this->assertStringNotContainsString('public', $header);
$this->assertStringNotContainsString('private', $header);
$this->assertStringContainsString('no-cache', $header);
$this->assertStringContainsString('no-store', $header);
$this->assertStringContainsString('must-revalidate', $header);
}
public function testPublicForm()
@ -44,10 +44,10 @@ class HTTPCacheControlIntegrationTest extends FunctionalTest
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/showpublicform');
$header = $response->getHeader('Cache-Control');
$this->assertFalse($response->isError());
$this->assertContains('public', $header);
$this->assertContains('must-revalidate', $header);
$this->assertNotContains('no-cache', $response->getHeader('Cache-Control'));
$this->assertNotContains('no-store', $response->getHeader('Cache-Control'));
$this->assertStringContainsString('public', $header);
$this->assertStringContainsString('must-revalidate', $header);
$this->assertStringNotContainsString('no-cache', $response->getHeader('Cache-Control'));
$this->assertStringNotContainsString('no-store', $response->getHeader('Cache-Control'));
}
public function testPrivateActionsError()
@ -56,9 +56,9 @@ class HTTPCacheControlIntegrationTest extends FunctionalTest
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/privateaction');
$header = $response->getHeader('Cache-Control');
$this->assertTrue($response->isError());
$this->assertContains('no-cache', $header);
$this->assertContains('no-store', $header);
$this->assertContains('must-revalidate', $header);
$this->assertStringContainsString('no-cache', $header);
$this->assertStringContainsString('no-store', $header);
$this->assertStringContainsString('must-revalidate', $header);
}
public function testPrivateActionsAuthenticated()
@ -68,10 +68,10 @@ class HTTPCacheControlIntegrationTest extends FunctionalTest
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/privateaction');
$header = $response->getHeader('Cache-Control');
$this->assertFalse($response->isError());
$this->assertContains('private', $header);
$this->assertContains('must-revalidate', $header);
$this->assertNotContains('no-cache', $header);
$this->assertNotContains('no-store', $header);
$this->assertStringContainsString('private', $header);
$this->assertStringContainsString('must-revalidate', $header);
$this->assertStringNotContainsString('no-cache', $header);
$this->assertStringNotContainsString('no-store', $header);
}
public function testPrivateCache()
@ -79,10 +79,10 @@ class HTTPCacheControlIntegrationTest extends FunctionalTest
$response = $this->get('HTTPCacheControlIntegrationTest_RuleController/privateaction');
$header = $response->getHeader('Cache-Control');
$this->assertFalse($response->isError());
$this->assertContains('private', $header);
$this->assertContains('must-revalidate', $header);
$this->assertNotContains('no-cache', $header);
$this->assertNotContains('no-store', $header);
$this->assertStringContainsString('private', $header);
$this->assertStringContainsString('must-revalidate', $header);
$this->assertStringNotContainsString('no-cache', $header);
$this->assertStringNotContainsString('no-store', $header);
}
public function testPublicCache()
@ -90,11 +90,11 @@ class HTTPCacheControlIntegrationTest extends FunctionalTest
$response = $this->get('HTTPCacheControlIntegrationTest_RuleController/publicaction');
$header = $response->getHeader('Cache-Control');
$this->assertFalse($response->isError());
$this->assertContains('public', $header);
$this->assertContains('must-revalidate', $header);
$this->assertNotContains('no-cache', $header);
$this->assertNotContains('no-store', $header);
$this->assertContains('max-age=9000', $header);
$this->assertStringContainsString('public', $header);
$this->assertStringContainsString('must-revalidate', $header);
$this->assertStringNotContainsString('no-cache', $header);
$this->assertStringNotContainsString('no-store', $header);
$this->assertStringContainsString('max-age=9000', $header);
}
public function testDisabledCache()
@ -102,10 +102,10 @@ class HTTPCacheControlIntegrationTest extends FunctionalTest
$response = $this->get('HTTPCacheControlIntegrationTest_RuleController/disabledaction');
$header = $response->getHeader('Cache-Control');
$this->assertFalse($response->isError());
$this->assertNotContains('public', $header);
$this->assertNotContains('private', $header);
$this->assertContains('no-cache', $header);
$this->assertContains('no-store', $header);
$this->assertContains('must-revalidate', $header);
$this->assertStringNotContainsString('public', $header);
$this->assertStringNotContainsString('private', $header);
$this->assertStringContainsString('no-cache', $header);
$this->assertStringContainsString('no-store', $header);
$this->assertStringContainsString('must-revalidate', $header);
}
}

View File

@ -52,11 +52,10 @@ class HTTPRequestTest extends SapphireTest
/**
* This test just asserts a warning is given if there is more than one wildcard parameter. Note that this isn't an
* enforcement of an API and we an add new behaviour in the future to allow many wildcard params if we want to
*
* @expectedException \PHPUnit_Framework_Error_Warning
*/
public function testWildCardWithFurtherParams()
{
$this->expectWarning();
$request = new HTTPRequest('GET', 'admin/crm/test');
// all parameters after the first wildcard parameter are ignored
$request->match('admin/$Action/$@/$Other/$*', true);

View File

@ -19,7 +19,7 @@ use SilverStripe\Dev\FunctionalTest;
*/
class HTTPTest extends FunctionalTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
// Set to disabled at null forcing level
@ -48,9 +48,9 @@ class HTTPTest extends FunctionalTest
HTTPCacheControlMiddleware::singleton()->setMaxAge(30);
$response = new HTTPResponse($body, 200);
$this->addCacheHeaders($response);
$this->assertContains('no-cache', $response->getHeader('Cache-Control'));
$this->assertContains('no-store', $response->getHeader('Cache-Control'));
$this->assertContains('must-revalidate', $response->getHeader('Cache-Control'));
$this->assertStringContainsString('no-cache', $response->getHeader('Cache-Control'));
$this->assertStringContainsString('no-store', $response->getHeader('Cache-Control'));
$this->assertStringContainsString('must-revalidate', $response->getHeader('Cache-Control'));
// Ensure max-age setting is respected in production.
HTTPCacheControlMiddleware::config()
@ -61,8 +61,8 @@ class HTTPTest extends FunctionalTest
HTTPCacheControlMiddleware::singleton()->setMaxAge(30);
$response = new HTTPResponse($body, 200);
$this->addCacheHeaders($response);
$this->assertContains('max-age=30', $response->getHeader('Cache-Control'));
$this->assertNotContains('max-age=0', $response->getHeader('Cache-Control'));
$this->assertStringContainsString('max-age=30', $response->getHeader('Cache-Control'));
$this->assertStringNotContainsString('max-age=0', $response->getHeader('Cache-Control'));
// Still "live": Ensure header's aren't overridden if already set (using purposefully different values).
$headers = [
@ -93,11 +93,11 @@ class HTTPTest extends FunctionalTest
// Vary set properly
$v = $response->getHeader('Vary');
$this->assertContains("X-Forwarded-Protocol", $v);
$this->assertContains("X-Requested-With", $v);
$this->assertNotContains("Cookie", $v);
$this->assertNotContains("User-Agent", $v);
$this->assertNotContains("Accept", $v);
$this->assertStringContainsString("X-Forwarded-Protocol", $v);
$this->assertStringContainsString("X-Requested-With", $v);
$this->assertStringNotContainsString("Cookie", $v);
$this->assertStringNotContainsString("User-Agent", $v);
$this->assertStringNotContainsString("Accept", $v);
// No vary
HTTPCacheControlMiddleware::singleton()
@ -124,7 +124,7 @@ class HTTPTest extends FunctionalTest
$response = new HTTPResponse('', 200);
$this->addCacheHeaders($response);
$header = $response->getHeader('Vary');
$this->assertContains('X-Foo', $header);
$this->assertStringContainsString('X-Foo', $header);
}
public function testDeprecatedCacheControlHandling()
@ -143,8 +143,8 @@ class HTTPTest extends FunctionalTest
$response = new HTTPResponse('', 200);
$this->addCacheHeaders($response);
$header = $response->getHeader('Cache-Control');
$this->assertContains('no-store', $header);
$this->assertContains('no-cache', $header);
$this->assertStringContainsString('no-store', $header);
$this->assertStringContainsString('no-cache', $header);
}
public function testDeprecatedCacheControlHandlingOnMaxAge()
@ -164,15 +164,13 @@ class HTTPTest extends FunctionalTest
$response = new HTTPResponse('', 200);
$this->addCacheHeaders($response);
$header = $response->getHeader('Cache-Control');
$this->assertContains('max-age=99', $header);
$this->assertStringContainsString('max-age=99', $header);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessageRegExp /Found unsupported legacy directives in HTTP\.cache_control: unknown/
*/
public function testDeprecatedCacheControlHandlingThrowsWithUnknownDirectives()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('/Found unsupported legacy directives in HTTP\.cache_control: unknown/');
/** @var Config */
Config::modify()->set(
HTTP::class,
@ -219,7 +217,7 @@ class HTTPTest extends FunctionalTest
sort($result);
sort($expected);
$this->assertInternalType('array', $result);
$this->assertIsArray($result);
$this->assertEquals($expected, $result, 'Test that all links within the content are found.');
}
@ -235,7 +233,7 @@ class HTTPTest extends FunctionalTest
$controller->setRequest($request);
$controller->pushCurrent();
try {
$this->assertContains(
$this->assertStringContainsString(
'relative/url?foo=bar',
HTTP::setGetVar('foo', 'bar'),
'Omitting a URL falls back to current URL'
@ -263,7 +261,7 @@ class HTTPTest extends FunctionalTest
'Absolute URL without path and multipe existing query params, overwriting an existing parameter'
);
$this->assertContains(
$this->assertStringContainsString(
'http://test.com/?foo=new',
HTTP::setGetVar('foo', 'new', 'http://test.com/?foo=&foo=old'),
'Absolute URL and empty query param'

View File

@ -17,14 +17,14 @@ use SilverStripe\Control\Util\IPUtils;
class IPUtilsTest extends SapphireTest
{
/**
* @dataProvider testIPv4Provider
* @dataProvider iPv4Provider
*/
public function testIPv4($matches, $remoteAddr, $cidr)
{
$this->assertSame($matches, IPUtils::checkIP($remoteAddr, $cidr));
}
public function testIPv4Provider()
public function iPv4Provider()
{
return [
[true, '192.168.1.1', '192.168.1.1'],
@ -43,7 +43,7 @@ class IPUtilsTest extends SapphireTest
}
/**
* @dataProvider testIPv6Provider
* @dataProvider iPv6Provider
*/
public function testIPv6($matches, $remoteAddr, $cidr)
{
@ -54,7 +54,7 @@ class IPUtilsTest extends SapphireTest
$this->assertSame($matches, IPUtils::checkIP($remoteAddr, $cidr));
}
public function testIPv6Provider()
public function iPv6Provider()
{
return [
[true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'],
@ -71,11 +71,11 @@ class IPUtilsTest extends SapphireTest
}
/**
* @expectedException \RuntimeException
* @requires extension sockets
*/
public function testAnIPv6WithOptionDisabledIPv6()
{
$this->expectException(\RuntimeException::class);
if (defined('AF_INET6')) {
$this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
}

View File

@ -16,7 +16,7 @@ class CanonicalURLMiddlewareTest extends SapphireTest
*/
protected $middleware;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -45,7 +45,7 @@ class CanonicalURLMiddlewareTest extends SapphireTest
$this->assertNotSame($mockResponse, $result, 'New response is created and returned');
$this->assertEquals(301, $result->getStatusCode(), 'Basic auth responses are redirected');
$this->assertContains('https://', $result->getHeader('Location'), 'HTTPS is in the redirect location');
$this->assertStringContainsString('https://', $result->getHeader('Location'), 'HTTPS is in the redirect location');
}
public function testMiddlewareDelegateIsReturnedWhenBasicAuthRedirectIsDisabled()

View File

@ -8,7 +8,7 @@ use SilverStripe\Dev\SapphireTest;
class HTTPCacheControlMiddlewareTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
// Set to disabled at null forcing level
@ -39,7 +39,7 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
$response = new HTTPResponse();
$cc->applyToResponse($response);
$this->assertContains('must-revalidate', $response->getHeader('cache-control'));
$this->assertStringContainsString('must-revalidate', $response->getHeader('cache-control'));
}
/**
@ -62,9 +62,9 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
if ($immutable) {
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
} else {
$this->assertContains('max-age=300', $response->getHeader('cache-control'));
$this->assertNotContains('no-cache', $response->getHeader('cache-control'));
$this->assertNotContains('no-store', $response->getHeader('cache-control'));
$this->assertStringContainsString('max-age=300', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('no-cache', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('no-store', $response->getHeader('cache-control'));
}
}
@ -78,9 +78,9 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
$response = new HTTPResponse();
$cc->applyToResponse($response);
$this->assertContains('max-age=300', $response->getHeader('cache-control'));
$this->assertNotContains('no-cache', $response->getHeader('cache-control'));
$this->assertNotContains('no-store', $response->getHeader('cache-control'));
$this->assertStringContainsString('max-age=300', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('no-cache', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('no-store', $response->getHeader('cache-control'));
}
public function testEnableCacheWithMaxAgeAppliesWhenLevelDoesNot()
@ -94,7 +94,7 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
$response = new HTTPResponse();
$cc->applyToResponse($response);
$this->assertContains('max-age=300', $response->getHeader('cache-control'));
$this->assertStringContainsString('max-age=300', $response->getHeader('cache-control'));
}
public function testPublicCacheWithMaxAge()
@ -107,10 +107,10 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
$response = new HTTPResponse();
$cc->applyToResponse($response);
$this->assertContains('max-age=300', $response->getHeader('cache-control'));
$this->assertStringContainsString('max-age=300', $response->getHeader('cache-control'));
// STATE_PUBLIC doesn't contain no-cache or no-store headers to begin with,
// so can't test their removal effectively
$this->assertNotContains('no-cache', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('no-cache', $response->getHeader('cache-control'));
}
public function testPublicCacheWithMaxAgeAppliesWhenLevelDoesNot()
@ -124,7 +124,7 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
$response = new HTTPResponse();
$cc->applyToResponse($response);
$this->assertContains('max-age=300', $response->getHeader('cache-control'));
$this->assertStringContainsString('max-age=300', $response->getHeader('cache-control'));
}
/**
@ -150,9 +150,9 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
if ($immutable) {
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
} else {
$this->assertContains('no-store', $response->getHeader('cache-control'));
$this->assertNotContains('max-age', $response->getHeader('cache-control'));
$this->assertNotContains('s-maxage', $response->getHeader('cache-control'));
$this->assertStringContainsString('no-store', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('max-age', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('s-maxage', $response->getHeader('cache-control'));
}
}
@ -179,9 +179,9 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
if ($immutable) {
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
} else {
$this->assertContains('no-cache', $response->getHeader('cache-control'));
$this->assertNotContains('max-age', $response->getHeader('cache-control'));
$this->assertNotContains('s-maxage', $response->getHeader('cache-control'));
$this->assertStringContainsString('no-cache', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('max-age', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('s-maxage', $response->getHeader('cache-control'));
}
}
@ -206,9 +206,9 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
if ($immutable) {
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
} else {
$this->assertContains('s-maxage=300', $response->getHeader('cache-control'));
$this->assertNotContains('no-cache', $response->getHeader('cache-control'));
$this->assertNotContains('no-store', $response->getHeader('cache-control'));
$this->assertStringContainsString('s-maxage=300', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('no-cache', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('no-store', $response->getHeader('cache-control'));
}
}
@ -233,9 +233,9 @@ class HTTPCacheControlMiddlewareTest extends SapphireTest
if ($immutable) {
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
} else {
$this->assertContains('must-revalidate', $response->getHeader('cache-control'));
$this->assertNotContains('max-age', $response->getHeader('cache-control'));
$this->assertNotContains('s-maxage', $response->getHeader('cache-control'));
$this->assertStringContainsString('must-revalidate', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('max-age', $response->getHeader('cache-control'));
$this->assertStringNotContainsString('s-maxage', $response->getHeader('cache-control'));
}
}

View File

@ -17,7 +17,7 @@ class RateLimitMiddlewareTest extends FunctionalTest
TestController::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
DBDatetime::set_mock_now('2017-09-27 00:00:00');

View File

@ -24,17 +24,17 @@ class RSSFeedTest extends SapphireTest
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description");
$content = $rssFeed->outputToBrowser();
$this->assertContains('<link>http://www.example.org/item-a/</link>', $content);
$this->assertContains('<link>http://www.example.com/item-b.html</link>', $content);
$this->assertContains('<link>http://www.example.com/item-c.html</link>', $content);
$this->assertStringContainsString('<link>http://www.example.org/item-a/</link>', $content);
$this->assertStringContainsString('<link>http://www.example.com/item-b.html</link>', $content);
$this->assertStringContainsString('<link>http://www.example.com/item-c.html</link>', $content);
$this->assertContains('<title>ItemA</title>', $content);
$this->assertContains('<title>ItemB</title>', $content);
$this->assertContains('<title>ItemC</title>', $content);
$this->assertStringContainsString('<title>ItemA</title>', $content);
$this->assertStringContainsString('<title>ItemB</title>', $content);
$this->assertStringContainsString('<title>ItemC</title>', $content);
$this->assertContains('<description>ItemA Content</description>', $content);
$this->assertContains('<description>ItemB Content</description>', $content);
$this->assertContains('<description>ItemC Content</description>', $content);
$this->assertStringContainsString('<description>ItemA Content</description>', $content);
$this->assertStringContainsString('<description>ItemB Content</description>', $content);
$this->assertStringContainsString('<description>ItemC Content</description>', $content);
// Feed #2 - put Content() into <title> and AltContent() into <description>
@ -48,13 +48,13 @@ class RSSFeedTest extends SapphireTest
);
$content = $rssFeed->outputToBrowser();
$this->assertContains('<title>ItemA Content</title>', $content);
$this->assertContains('<title>ItemB Content</title>', $content);
$this->assertContains('<title>ItemC Content</title>', $content);
$this->assertStringContainsString('<title>ItemA Content</title>', $content);
$this->assertStringContainsString('<title>ItemB Content</title>', $content);
$this->assertStringContainsString('<title>ItemC Content</title>', $content);
$this->assertContains('<description>ItemA AltContent</description>', $content);
$this->assertContains('<description>ItemB AltContent</description>', $content);
$this->assertContains('<description>ItemC AltContent</description>', $content);
$this->assertStringContainsString('<description>ItemA AltContent</description>', $content);
$this->assertStringContainsString('<description>ItemB AltContent</description>', $content);
$this->assertStringContainsString('<description>ItemC AltContent</description>', $content);
}
public function testLinkEncoding()
@ -62,7 +62,7 @@ class RSSFeedTest extends SapphireTest
$list = new ArrayList();
$rssFeed = new RSSFeed($list, "http://www.example.com/?param1=true&param2=true", "Test RSS Feed");
$content = $rssFeed->outputToBrowser();
$this->assertContains('<link>http://www.example.com/?param1=true&amp;param2=true', $content);
$this->assertStringContainsString('<link>http://www.example.com/?param1=true&amp;param2=true', $content);
}
public function testRSSFeedWithShortcode()
@ -73,11 +73,11 @@ class RSSFeedTest extends SapphireTest
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description");
$content = $rssFeed->outputToBrowser();
$this->assertContains('<link>http://www.example.org/item-d.html</link>', $content);
$this->assertStringContainsString('<link>http://www.example.org/item-d.html</link>', $content);
$this->assertContains('<title>ItemD</title>', $content);
$this->assertStringContainsString('<title>ItemD</title>', $content);
$this->assertContains(
$this->assertStringContainsString(
'<description><![CDATA[<p>ItemD Content test shortcode output</p>]]></description>',
$content
);
@ -92,14 +92,14 @@ class RSSFeedTest extends SapphireTest
$rssFeed->setTemplate('RSSFeedTest');
$content = $rssFeed->outputToBrowser();
$this->assertContains('<title>Test Custom Template</title>', $content);
$this->assertStringContainsString('<title>Test Custom Template</title>', $content);
$rssFeed->setTemplate(null);
$content = $rssFeed->outputToBrowser();
$this->assertNotContains('<title>Test Custom Template</title>', $content);
$this->assertStringNotContainsString('<title>Test Custom Template</title>', $content);
}
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Config::modify()->set(Director::class, 'alternate_base_url', '/');
@ -116,7 +116,7 @@ class RSSFeedTest extends SapphireTest
);
}
protected function tearDown()
protected function tearDown(): void
{
parent::tearDown();
$_SERVER['HTTP_HOST'] = self::$original_host;

View File

@ -357,7 +357,7 @@ class RequestHandlingTest extends FunctionalTest
$response = $this->post('ControllerFormWithAllowedActions/Form', $data);
$this->assertEquals(403, $response->getStatusCode());
// Note: Looks for a specific 403 thrown by Form->httpSubmission(), not RequestHandler->handleRequest()
$this->assertContains('not allowed on form', $response->getBody());
$this->assertStringContainsString('not allowed on form', $response->getBody());
}
public function testActionHandlingOnField()

View File

@ -18,10 +18,10 @@ class SessionTest extends SapphireTest
*/
protected $session = null;
protected function setUp()
protected function setUp(): void
{
$this->session = new Session([]);
return parent::setUp();
parent::setUp();
}
/**
@ -107,11 +107,11 @@ class SessionTest extends SapphireTest
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @expectedException BadMethodCallException
* @expectedExceptionMessage Session has already started
*/
public function testStartErrorsWhenStartingTwice()
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Session has already started');
$req = new HTTPRequest('GET', '/');
$session = new Session(null); // unstarted session
$session->start($req);

View File

@ -11,7 +11,7 @@ use SilverStripe\Dev\SapphireTest;
class SimpleResourceURLGeneratorTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Director::config()->set(

View File

@ -13,7 +13,7 @@ use Symfony\Component\Cache\Simple\MemcachedCache;
class CacheTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -11,7 +11,7 @@ use Symfony\Component\Cache\Simple\ArrayCache;
class RateLimiterTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
DBDatetime::set_mock_now('2017-09-27 00:00:00');

View File

@ -3,6 +3,7 @@
namespace SilverStripe\Core\Tests;
use DateTime;
use Exception;
use ReflectionException;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Tests\ClassInfoTest\BaseClass;
@ -42,7 +43,7 @@ class ClassInfoTest extends SapphireTest
ExtendTest3::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
ClassInfo::reset_db_cache();
@ -106,8 +107,8 @@ class ClassInfoTest extends SapphireTest
public function testNonClassName()
{
$this->expectException(ReflectionException::class);
$this->expectExceptionMessageRegExp('/Class "?IAmAClassThatDoesNotExist"? does not exist/');
$this->expectException(Exception::class);
$this->expectExceptionMessageMatches('/Class "?IAmAClassThatDoesNotExist"? does not exist/');
$this->assertEquals('IAmAClassThatDoesNotExist', ClassInfo::class_name('IAmAClassThatDoesNotExist'));
}

View File

@ -19,14 +19,14 @@ class ConvertTest extends SapphireTest
private $previousLocaleSetting = null;
public function setUp()
protected function setUp(): void
{
parent::setUp();
// clear the previous locale setting
$this->previousLocaleSetting = null;
}
public function tearDown()
protected function tearDown(): void
{
parent::tearDown();
// If a test sets the locale, reset it on teardown
@ -242,7 +242,7 @@ PHP
$this->assertEquals(3, count($decoded), '3 items in the decoded array');
$this->assertContains('Bloggs', $decoded, 'Contains "Bloggs" value in decoded array');
$this->assertContains('Jones', $decoded, 'Contains "Jones" value in decoded array');
$this->assertContains('Structure', $decoded['My']['Complicated']);
$this->assertStringContainsString('Structure', $decoded['My']['Complicated']);
}
/**

View File

@ -15,7 +15,7 @@ class CoreTest extends SapphireTest
protected $tempPath;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->tempPath = Director::baseFolder() . DIRECTORY_SEPARATOR . 'silverstripe-cache';
@ -51,7 +51,7 @@ class CoreTest extends SapphireTest
}
}
protected function tearDown()
protected function tearDown(): void
{
parent::tearDown();
$user = TempFolder::getTempFolderUsername();

View File

@ -2,7 +2,6 @@
namespace SilverStripe\Core\Tests\Injector;
use InvalidArgumentException;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Factory;
use SilverStripe\Core\Injector\Injector;
@ -44,14 +43,14 @@ class InjectorTest extends SapphireTest
protected $nestingLevel = 0;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->nestingLevel = 0;
}
protected function tearDown()
protected function tearDown(): void
{
while ($this->nestingLevel > 0) {
@ -910,11 +909,9 @@ class InjectorTest extends SapphireTest
);
}
/**
* @expectedException InvalidArgumentException
*/
public function testNonExistentMethods()
{
$this->expectException(\InvalidArgumentException::class);
$injector = new Injector();
$config = [
'TestService' => [
@ -929,11 +926,9 @@ class InjectorTest extends SapphireTest
$item = $injector->get('TestService');
}
/**
* @expectedException InvalidArgumentException
*/
public function testProtectedMethods()
{
$this->expectException(\InvalidArgumentException::class);
$injector = new Injector();
$config = [
'TestService' => [
@ -948,11 +943,9 @@ class InjectorTest extends SapphireTest
$item = $injector->get('TestService');
}
/**
* @expectedException InvalidArgumentException
*/
public function testTooManyArrayValues()
{
$this->expectException(\InvalidArgumentException::class);
$injector = new Injector();
$config = [
'TestService' => [
@ -967,11 +960,9 @@ class InjectorTest extends SapphireTest
$item = $injector->get('TestService');
}
/**
* @expectedException \SilverStripe\Core\Injector\InjectorNotFoundException
*/
public function testGetThrowsOnNotFound()
{
$this->expectException(InjectorNotFoundException::class);
$injector = new Injector();
$injector->get('UnknownService');
}

View File

@ -32,7 +32,7 @@ class ClassLoaderTest extends SapphireTest
*/
protected $testManifest2;
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -7,12 +7,10 @@ use PhpParser\Error;
class ClassManifestErrorHandlerTest extends SapphireTest
{
/**
* @expectedException \PhpParser\Error
* @expectedExceptionMessage my error in /my/path
*/
public function testIncludesPathname()
{
$this->expectException(Error::class);
$this->expectExceptionMessage('my error in /my/path');
$h = new ClassManifestErrorHandler('/my/path');
$e = new Error('my error');
$h->handleError($e);

View File

@ -27,7 +27,7 @@ class ClassManifestTest extends SapphireTest
*/
protected $manifestTests;
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -13,7 +13,7 @@ use SilverStripe\Dev\SapphireTest;
class ConfigManifestTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -22,7 +22,7 @@ class ConfigManifestTest extends SapphireTest
ModuleLoader::inst()->pushManifest($moduleManifest);
}
protected function tearDown()
protected function tearDown(): void
{
ModuleLoader::inst()->popManifest();
parent::tearDown();
@ -225,7 +225,7 @@ class ConfigManifestTest extends SapphireTest
'Fragment is included if both blocks succeed.'
);
}
public function testExtensionLoaded()
{
$config = $this->getConfigFixtureValue('ExtensionLoaded');

View File

@ -26,7 +26,7 @@ class ManifestFileFinderTest extends SapphireTest
* @param array $expect
* @param string $message
*/
public function assertFinderFinds(ManifestFileFinder $finder, $base, $expect, $message = null)
public function assertFinderFinds(ManifestFileFinder $finder, $base, $expect, $message = '')
{
if (!$base) {
$base = $this->defaultBase;

View File

@ -18,7 +18,7 @@ class ModuleManifestTest extends SapphireTest
*/
protected $manifest;
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -18,7 +18,7 @@ class ModuleResourceTest extends SapphireTest
*/
protected $manifest;
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -8,6 +8,7 @@ use SilverStripe\Core\Manifest\ClassManifest;
use SilverStripe\Core\Manifest\ClassLoader;
use SilverStripe\Dev\SapphireTest;
use ReflectionMethod;
use SilverStripe\ORM\DataQuery;
use SilverStripe\Security\PermissionProvider;
/**
@ -25,7 +26,7 @@ class NamespacedClassManifestTest extends SapphireTest
*/
protected $manifest;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -35,7 +36,7 @@ class NamespacedClassManifestTest extends SapphireTest
ClassLoader::inst()->pushManifest($this->manifest, false);
}
protected function tearDown()
protected function tearDown(): void
{
parent::tearDown();
ClassLoader::inst()->popManifest();
@ -43,8 +44,9 @@ class NamespacedClassManifestTest extends SapphireTest
public function testClassInfoIsCorrect()
{
$class = 'SilverStripe\\Framework\\Tests\\ClassI';
$this->assertContains(
'SilverStripe\\Framework\\Tests\\ClassI',
$class,
ClassInfo::implementorsOf(PermissionProvider::class)
);
@ -53,8 +55,13 @@ class NamespacedClassManifestTest extends SapphireTest
// including all core classes
$method = new ReflectionMethod($this->manifest, 'coalesceDescendants');
$method->setAccessible(true);
$method->invoke($this->manifest, ModelAdmin::class);
$this->assertContains('SilverStripe\\Framework\\Tests\\ClassI', ClassInfo::subclassesFor(ModelAdmin::class));
$method->invoke($this->manifest, DataQuery::class);
$classes = ClassInfo::subclassesFor(DataQuery::class);
$this->assertContains(
$class,
$classes,
$class . ' not contained in [' . implode(',', $classes) . ']'
);
}
public function testGetItemPath()

View File

@ -12,7 +12,7 @@ class PrioritySorterTest extends SapphireTest
*/
protected $sorter;
public function setUp()
protected function setUp(): void
{
parent::setUp();
$modules = [

View File

@ -33,7 +33,7 @@ class ThemeResourceLoaderTest extends SapphireTest
/**
* Set up manifest before each test
*/
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -60,7 +60,7 @@ class ThemeResourceLoaderTest extends SapphireTest
ThemeResourceLoader::flush();
}
protected function tearDown()
protected function tearDown(): void
{
ModuleLoader::inst()->popManifest();
parent::tearDown();

View File

@ -81,9 +81,9 @@ class VersionProviderTest extends SapphireTest
'silverstripe/framework' => 'Framework'
]);
$result = $this->getMockProvider()->getVersion();
$this->assertNotContains('SiteConfig: ', $result);
$this->assertContains('Framework: ', $result);
$this->assertNotContains(', ', $result);
$this->assertStringNotContainsString('SiteConfig: ', $result);
$this->assertStringContainsString('Framework: ', $result);
$this->assertStringNotContainsString(', ', $result);
}
public function testGetVersionNoRecipe()
@ -93,7 +93,7 @@ class VersionProviderTest extends SapphireTest
Config::modify()->set(VersionProvider::class, 'modules', []);
$result = $provider->getVersion();
$this->assertContains('Framework: 1.2.3', $result);
$this->assertStringContainsString('Framework: 1.2.3', $result);
Config::modify()->set(VersionProvider::class, 'modules', [
'silverstripe/framework' => 'Framework',
@ -102,10 +102,10 @@ class VersionProviderTest extends SapphireTest
'silverstripe/recipe-cms' => 'CMS Recipe',
]);
$result = $provider->getVersion();
$this->assertNotContains('Framework: 1.2.3', $result);
$this->assertContains('CMS: 4.5.6', $result);
$this->assertNotContains('Core Recipe: 7.7.7', $result);
$this->assertNotContains('CMS Recipe: 8.8.8', $result);
$this->assertStringNotContainsString('Framework: 1.2.3', $result);
$this->assertStringContainsString('CMS: 4.5.6', $result);
$this->assertStringNotContainsString('Core Recipe: 7.7.7', $result);
$this->assertStringNotContainsString('CMS Recipe: 8.8.8', $result);
}
public function testGetVersionRecipeCore()
@ -119,10 +119,10 @@ class VersionProviderTest extends SapphireTest
'silverstripe/recipe-cms' => 'CMS Recipe',
]);
$result = $provider->getVersion();
$this->assertNotContains('Framework: 1.2.3', $result);
$this->assertNotContains('Core Recipe: 7.7.7', $result);
$this->assertContains('CMS: 4.5.6', $result);
$this->assertNotContains('CMS Recipe: 8.8.8', $result);
$this->assertStringNotContainsString('Framework: 1.2.3', $result);
$this->assertStringNotContainsString('Core Recipe: 7.7.7', $result);
$this->assertStringContainsString('CMS: 4.5.6', $result);
$this->assertStringNotContainsString('CMS Recipe: 8.8.8', $result);
}
public function testGetVersionRecipeCmsCore()
@ -139,11 +139,11 @@ class VersionProviderTest extends SapphireTest
]);
$result = $provider->getVersion();
$this->assertNotContains('Framework: 1.2.3', $result);
$this->assertNotContains('CMS: 4.5.6', $result);
$this->assertNotContains('Core Recipe: 7.7.7', $result);
$this->assertContains('CMS Recipe: 8.8.8', $result);
$this->assertNotContains('CWP: 9.9.9', $result);
$this->assertStringNotContainsString('Framework: 1.2.3', $result);
$this->assertStringNotContainsString('CMS: 4.5.6', $result);
$this->assertStringNotContainsString('Core Recipe: 7.7.7', $result);
$this->assertStringContainsString('CMS Recipe: 8.8.8', $result);
$this->assertStringNotContainsString('CWP: 9.9.9', $result);
Config::modify()->set(VersionProvider::class, 'modules', [
'silverstripe/framework' => 'Framework',
@ -153,11 +153,11 @@ class VersionProviderTest extends SapphireTest
'cwp/cwp-core' => 'CWP',
]);
$result = $provider->getVersion();
$this->assertNotContains('Framework: 1.2.3', $result);
$this->assertNotContains('CMS: 4.5.6', $result);
$this->assertNotContains('Core Recipe: 7.7.7', $result);
$this->assertContains('CMS Recipe:', $result);
$this->assertContains('CWP: 9.9.9', $result);
$this->assertStringNotContainsString('Framework: 1.2.3', $result);
$this->assertStringNotContainsString('CMS: 4.5.6', $result);
$this->assertStringNotContainsString('Core Recipe: 7.7.7', $result);
$this->assertStringContainsString('CMS Recipe:', $result);
$this->assertStringContainsString('CWP: 9.9.9', $result);
}
public function testGetModulesFromComposerLock()
@ -186,6 +186,6 @@ class VersionProviderTest extends SapphireTest
]);
$result = $mock->getVersion();
$this->assertContains('Some Package: 1.2.3', $result);
$this->assertStringContainsString('Some Package: 1.2.3', $result);
}
}

View File

@ -4,7 +4,7 @@ namespace SilverStripe\Framework\Tests;
//whitespace here is important for tests, please don't change it
/** @skipUpgrade */
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\ORM\DataQuery;
/** @skipUpgrade */
use SilverStripe\Control\Controller as Cont ;
/** @skipUpgrade */
@ -15,5 +15,5 @@ use silverstripe\test\ClassA;
use \SilverStripe\Core\ClassInfo;
/** @skipUpgrade */
class ClassI extends ModelAdmin implements P {
class ClassI extends DataQuery implements P {
}

View File

@ -12,7 +12,7 @@ class MemoryLimitTest extends SapphireTest
protected $origMemLimit;
protected $origTimeLimit;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -29,7 +29,7 @@ class MemoryLimitTest extends SapphireTest
}
}
protected function tearDown()
protected function tearDown(): void
{
if (!in_array('suhosin', get_loaded_extensions())) {
ini_set('memory_limit', $this->origMemLimit);
@ -42,23 +42,26 @@ class MemoryLimitTest extends SapphireTest
public function testIncreaseMemoryLimitTo()
{
ini_set('memory_limit', '64M');
// ini_set('memory_limit', '64M');
// current memory usage in travis is 197M, can't ini_set this down to 64M
// Using a higher memory limit instead
ini_set('memory_limit', '230M');
Environment::setMemoryLimitMax('256M');
// It can go up
Environment::increaseMemoryLimitTo('128M');
$this->assertEquals('128M', ini_get('memory_limit'));
Environment::increaseMemoryLimitTo('240M');
$this->assertEquals('240M', ini_get('memory_limit'));
// But not down
Environment::increaseMemoryLimitTo('64M');
$this->assertEquals('128M', ini_get('memory_limit'));
Environment::increaseMemoryLimitTo('220M');
$this->assertEquals('240M', ini_get('memory_limit'));
// Test the different kinds of syntaxes
Environment::increaseMemoryLimitTo(1024*1024*200);
$this->assertEquals('200M', ini_get('memory_limit'));
Environment::increaseMemoryLimitTo(1024*1024*250);
$this->assertEquals('250M', ini_get('memory_limit'));
Environment::increaseMemoryLimitTo('109600K');
$this->assertEquals('200M', ini_get('memory_limit'));
$this->assertEquals('250M', ini_get('memory_limit'));
// Attempting to increase past max size only sets to max
Environment::increaseMemoryLimitTo('1G');

View File

@ -31,7 +31,7 @@ use SilverStripe\Versioned\Versioned;
class ObjectTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Injector::inst()->unregisterObjects([

View File

@ -68,7 +68,7 @@ class PathTest extends SapphireTest
*/
public function testJoinPathsErrors($args, $error)
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($error);
Path::join($args);
}

View File

@ -9,7 +9,7 @@ use SilverStripe\Dev\SapphireTest;
*/
class PhpSyntaxTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->markTestSkipped('This needs to be written to include only core php files, not test/thirdparty files');

View File

@ -13,7 +13,7 @@ class BulkLoaderResultTest extends SapphireTest
Player::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Player::create(['Name' => 'Vincent', 'Status' => 'Available'])->write();

View File

@ -10,7 +10,7 @@ class CLIDebugViewTest extends SapphireTest
{
protected $caller = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -15,7 +15,7 @@ class CSVParserTest extends SapphireTest
*/
protected $csvPath = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->csvPath = __DIR__ . '/CsvBulkLoaderTest/csv/';

View File

@ -31,7 +31,7 @@ class CsvBulkLoaderTest extends SapphireTest
*/
protected $csvPath = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->csvPath = __DIR__ . '/CsvBulkLoaderTest/csv/';

View File

@ -10,7 +10,7 @@ class DebugViewTest extends SapphireTest
{
protected $caller = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -2,7 +2,6 @@
namespace SilverStripe\Dev\Tests;
use PHPUnit_Framework_Error;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\Tests\DeprecationTest\TestDeprecation;
@ -12,7 +11,7 @@ class DeprecationTest extends SapphireTest
static $originalVersionInfo;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -21,7 +20,7 @@ class DeprecationTest extends SapphireTest
Deprecation::set_enabled(true);
}
protected function tearDown()
protected function tearDown(): void
{
Deprecation::restore_settings(self::$originalVersionInfo);
parent::tearDown();
@ -33,11 +32,9 @@ class DeprecationTest extends SapphireTest
$this->assertNull(Deprecation::notice('2.0', 'Deprecation test failed'));
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testEqualVersionTriggersNotice()
{
$this->expectError();
Deprecation::notification_version('2.0.0');
Deprecation::notice('2.0.0', 'Deprecation test passed');
}
@ -49,11 +46,9 @@ class DeprecationTest extends SapphireTest
$this->assertNull(Deprecation::notice('2.0.0', 'Deprecation test failed'));
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testGreaterVersionTriggersNotice()
{
$this->expectError();
Deprecation::notification_version('3.0.0');
Deprecation::notice('2.0', 'Deprecation test passed');
}
@ -65,11 +60,9 @@ class DeprecationTest extends SapphireTest
$this->callThatOriginatesFromFramework();
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testMatchingModuleNotifcationVersionAffectsNotice()
{
$this->expectError();
Deprecation::notification_version('1.0.0');
Deprecation::notification_version('3.0.0', 'silverstripe/framework');
$this->callThatOriginatesFromFramework();
@ -83,32 +76,26 @@ class DeprecationTest extends SapphireTest
);
}
/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage DeprecationTest->testScopeMethod is deprecated. Method scope
*/
public function testScopeMethod()
{
$this->expectError();
$this->expectErrorMessage('DeprecationTest->testScopeMethod is deprecated. Method scope');
Deprecation::notification_version('2.0.0');
Deprecation::notice('2.0.0', 'Method scope', Deprecation::SCOPE_METHOD);
}
/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage DeprecationTest is deprecated. Class scope
*/
public function testScopeClass()
{
$this->expectError();
$this->expectErrorMessage('DeprecationTest is deprecated. Class scope');
Deprecation::notification_version('2.0.0');
Deprecation::notice('2.0.0', 'Class scope', Deprecation::SCOPE_CLASS);
}
/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage Global scope
*/
public function testScopeGlobal()
{
$this->expectError();
$this->expectErrorMessage('Global scope');
Deprecation::notification_version('2.0.0');
Deprecation::notice('2.0.0', 'Global scope', Deprecation::SCOPE_GLOBAL);
}

View File

@ -14,7 +14,7 @@ use SilverStripe\Dev\Tests\DevAdminControllerTest\Controller1;
class DevAdminControllerTest extends FunctionalTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -43,8 +43,8 @@ class DevAdminControllerTest extends FunctionalTest
{
// Check for the controller running from the registered url above
// (we use contains rather than equals because sometimes you get a warning)
$this->assertContains(Controller1::OK_MSG, $this->getCapture('/dev/x1'));
$this->assertContains(Controller1::OK_MSG, $this->getCapture('/dev/x1/y1'));
$this->assertStringContainsString(Controller1::OK_MSG, $this->getCapture('/dev/x1'));
$this->assertStringContainsString(Controller1::OK_MSG, $this->getCapture('/dev/x1/y1'));
}
public function testGoodRegisteredControllerStatus()

View File

@ -144,12 +144,10 @@ class FixtureBlueprintTest extends SapphireTest
$this->assertNotNull($obj2->HasManyRelation()->find('ID', $relation2->ID));
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage No fixture definitions found
*/
public function testCreateWithInvalidRelationName()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('No fixture definitions found');
$blueprint = new FixtureBlueprint(TestDataObject::class);
$obj = $blueprint->createObject(
@ -165,12 +163,10 @@ class FixtureBlueprintTest extends SapphireTest
);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage No fixture definitions found
*/
public function testCreateWithInvalidRelationIdentifier()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('No fixture definitions found');
$blueprint = new FixtureBlueprint(TestDataObject::class);
$obj = $blueprint->createObject(
@ -186,12 +182,10 @@ class FixtureBlueprintTest extends SapphireTest
);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid format
*/
public function testCreateWithInvalidRelationFormat()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid format');
$factory = new FixtureFactory();
$blueprint = new FixtureBlueprint(TestDataObject::class);

View File

@ -18,7 +18,7 @@ class SSListExporterTest extends SapphireTest
*/
private $exporter;
public function setUp()
protected function setUp(): void
{
parent::setUp();
$this->exporter = new SSListExporter();

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Dev\Tests;
use PHPUnit\Framework\ExpectationFailedException;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\ArrayList;
use SilverStripe\Security\Member;
@ -122,11 +123,10 @@ class SapphireTestTest extends SapphireTest
* @param $itemsForList
*
* @testdox assertion assertListAllMatch fails when not all items are matching
*
* @expectedException \PHPUnit_Framework_ExpectationFailedException
*/
public function testAssertListAllMatchFailsWhenNotMatchingAllItems($match, $itemsForList)
{
$this->expectException(ExpectationFailedException::class);
$list = $this->generateArrayListFromItems($itemsForList);
$this->assertListAllMatch($match, $list);
@ -156,11 +156,10 @@ class SapphireTestTest extends SapphireTest
*
* @param $matches
* @param $itemsForList array
*
* @expectedException \PHPUnit_Framework_ExpectationFailedException
*/
public function testAssertListContainsFailsIfListDoesNotContainMatch($matches, $itemsForList)
{
$this->expectException(ExpectationFailedException::class);
$list = $this->generateArrayListFromItems($itemsForList);
$list->push(Member::create(['FirstName' => 'Foo', 'Surname' => 'Foo']));
$list->push(Member::create(['FirstName' => 'Bar', 'Surname' => 'Bar']));
@ -191,11 +190,10 @@ class SapphireTestTest extends SapphireTest
* @param $itemsForList
*
* @testdox assertion assertListNotContains throws a exception when a matching item is found in the list
*
* @expectedException \PHPUnit_Framework_ExpectationFailedException
*/
public function testAssertListNotContainsFailsWhenListContainsAMatch($matches, $itemsForList)
{
$this->expectException(ExpectationFailedException::class);
$list = $this->generateArrayListFromItems($itemsForList);
$list->push(Member::create(['FirstName' => 'Foo', 'Surname' => 'Foo']));
$list->push(Member::create(['FirstName' => 'Bar', 'Surname' => 'Bar']));
@ -224,11 +222,10 @@ class SapphireTestTest extends SapphireTest
*
* @param $matches
* @param $itemsForList
*
* @expectedException \PHPUnit_Framework_ExpectationFailedException
*/
public function testAssertListEqualsFailsOnNonEqualLists($matches, $itemsForList)
{
$this->expectException(ExpectationFailedException::class);
$list = $this->generateArrayListFromItems($itemsForList);
$this->assertListEquals($matches, $list);

View File

@ -45,11 +45,9 @@ class YamlFixtureTest extends SapphireTest
$this->assertNull($obj->getFixtureFile());
}
/**
* @expectedException InvalidArgumentException
*/
public function testFailsWithInvalidFixturePath()
{
$this->expectException(\InvalidArgumentException::class);
$invalidPath = ltrim(FRAMEWORK_DIR . '/tests/testing/invalid.yml', '/');
$obj = Injector::inst()->create(YamlFixture::class, $invalidPath);
}

View File

@ -26,7 +26,7 @@ class CheckboxSetFieldMulitEnumTest extends SapphireTest
}
}
public function setUp()
protected function setUp(): void
{
if (!(DB::get_conn() instanceof MySQLDatabase)) {
$this->markTestSkipped('DBMultiEnum only supported by MySQL');
@ -35,7 +35,7 @@ class CheckboxSetFieldMulitEnumTest extends SapphireTest
parent::setUp();
}
public function tearDown()
protected function tearDown(): void
{
if (!(DB::get_conn() instanceof MySQLDatabase)) {
return;

View File

@ -359,13 +359,13 @@ class CheckboxSetFieldTest extends SapphireTest
]
);
$fieldHTML = (string)$field1->Field();
$this->assertContains('One', $fieldHTML);
$this->assertContains('Two &amp; Three', $fieldHTML);
$this->assertNotContains('Two & Three', $fieldHTML);
$this->assertContains('Four &amp; Five &amp; Six', $fieldHTML);
$this->assertNotContains('Four & Five & Six', $fieldHTML);
$this->assertContains('&lt;firstname&gt;', $fieldHTML);
$this->assertNotContains('<firstname>', $fieldHTML);
$this->assertStringContainsString('One', $fieldHTML);
$this->assertStringContainsString('Two &amp; Three', $fieldHTML);
$this->assertStringNotContainsString('Two & Three', $fieldHTML);
$this->assertStringContainsString('Four &amp; Five &amp; Six', $fieldHTML);
$this->assertStringNotContainsString('Four & Five & Six', $fieldHTML);
$this->assertStringContainsString('&lt;firstname&gt;', $fieldHTML);
$this->assertStringNotContainsString('<firstname>', $fieldHTML);
}
/**

View File

@ -134,9 +134,9 @@ class CompositeFieldTest extends SapphireTest
$field->setColumnCount(3);
$result = $field->extraClass();
$this->assertContains('field', $result, 'Default class was not added');
$this->assertContains('CompositeField', $result, 'Default class was not added');
$this->assertContains('multicolumn', $result, 'Multi column field did not have extra class added');
$this->assertStringContainsString('field', $result, 'Default class was not added');
$this->assertStringContainsString('CompositeField', $result, 'Default class was not added');
$this->assertStringContainsString('multicolumn', $result, 'Multi column field did not have extra class added');
}
public function testGetAttributes()
@ -163,7 +163,7 @@ class CompositeFieldTest extends SapphireTest
public function testCollateDataFieldsThrowsErrorOnDuplicateChildren()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp(
$this->expectExceptionMessageMatches(
"/a field called 'Test' appears twice in your form.*TextField.*TextField/"
);
@ -266,8 +266,8 @@ class CompositeFieldTest extends SapphireTest
$field->setName('TestComposite');
$result = $field->debug();
$this->assertContains(CompositeField::class . ' (TestComposite)', $result);
$this->assertContains('TestTextField', $result);
$this->assertContains('<ul', $result, 'Result should be formatted as a <ul>');
$this->assertStringContainsString(CompositeField::class . ' (TestComposite)', $result);
$this->assertStringContainsString('TestTextField', $result);
$this->assertStringContainsString('<ul', $result, 'Result should be formatted as a <ul>');
}
}

View File

@ -16,7 +16,7 @@ class ConfirmedPasswordFieldTest extends SapphireTest
{
protected $usesDatabase = true;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -67,12 +67,12 @@ class ConfirmedPasswordFieldTest extends SapphireTest
//hide by default and display show/hide toggle button
$field = new ConfirmedPasswordField('Test', 'Testing', 'valueA', null, true);
$fieldHTML = $field->Field();
$this->assertContains(
$this->assertStringContainsString(
"showOnClickContainer",
$fieldHTML,
"Test class for hiding/showing the form contents is set"
);
$this->assertContains(
$this->assertStringContainsString(
"showOnClick",
$fieldHTML,
"Test class for hiding/showing the form contents is set"
@ -81,12 +81,12 @@ class ConfirmedPasswordFieldTest extends SapphireTest
//show all by default
$field = new ConfirmedPasswordField('Test', 'Testing', 'valueA', null, false);
$fieldHTML = $field->Field();
$this->assertNotContains(
$this->assertStringNotContainsString(
"showOnClickContainer",
$fieldHTML,
"Test class for hiding/showing the form contents is set"
);
$this->assertNotContains(
$this->assertStringNotContainsString(
"showOnClick",
$fieldHTML,
"Test class for hiding/showing the form contents is set"
@ -200,7 +200,7 @@ class ConfirmedPasswordFieldTest extends SapphireTest
$this->assertSame($expectValid, $result, 'Validate method should return its result');
$this->assertSame($expectValid, $validator->getResult()->isValid());
if ($expectedMessage) {
$this->assertContains($expectedMessage, $validator->getResult()->serialize());
$this->assertStringContainsString($expectedMessage, $validator->getResult()->serialize());
}
}
@ -233,7 +233,7 @@ class ConfirmedPasswordFieldTest extends SapphireTest
$this->assertFalse($result, 'Validate method should return its result');
$this->assertFalse($validator->getResult()->isValid());
$this->assertContains(
$this->assertStringContainsString(
'Passwords must have at least one digit and one alphanumeric character',
$validator->getResult()->serialize()
);
@ -252,7 +252,7 @@ class ConfirmedPasswordFieldTest extends SapphireTest
$this->assertFalse($result, 'Validate method should return its result');
$this->assertFalse($validator->getResult()->isValid());
$this->assertContains(
$this->assertStringContainsString(
'You must enter your current password',
$validator->getResult()->serialize()
);
@ -274,7 +274,7 @@ class ConfirmedPasswordFieldTest extends SapphireTest
$this->assertFalse($result, 'Validate method should return its result');
$this->assertFalse($validator->getResult()->isValid());
$this->assertContains(
$this->assertStringContainsString(
'You must be logged in to change your password',
$validator->getResult()->serialize()
);
@ -300,7 +300,7 @@ class ConfirmedPasswordFieldTest extends SapphireTest
$this->assertFalse($result, 'Validate method should return its result');
$this->assertFalse($validator->getResult()->isValid());
$this->assertContains(
$this->assertStringContainsString(
'The current password you have entered is not correct',
$validator->getResult()->serialize()
);
@ -357,7 +357,7 @@ class ConfirmedPasswordFieldTest extends SapphireTest
$this->assertInstanceOf(ReadonlyField::class, $result);
$this->assertSame('Change it', $result->Title());
$this->assertContains('***', $result->Value());
$this->assertStringContainsString('***', $result->Value());
}
public function testPerformDisabledTransformation()

View File

@ -13,9 +13,9 @@ class CurrencyFieldDisabledTest extends SapphireTest
$field = new CurrencyField_Disabled('Test', '', '$5.00');
$result = $field->Field();
$this->assertContains('<input', $result, 'An input should be rendered');
$this->assertContains('disabled', $result, 'The input should be disabled');
$this->assertContains('$5.00', $result, 'The value should be rendered');
$this->assertStringContainsString('<input', $result, 'An input should be rendered');
$this->assertStringContainsString('disabled', $result, 'The input should be disabled');
$this->assertStringContainsString('$5.00', $result, 'The value should be rendered');
}
/**
@ -27,8 +27,8 @@ class CurrencyFieldDisabledTest extends SapphireTest
$field = new CurrencyField_Disabled('Test', '', '€5.00');
$result = $field->Field();
$this->assertContains('<input', $result, 'An input should be rendered');
$this->assertContains('disabled', $result, 'The input should be disabled');
$this->assertContains('€5.00', $result, 'The value should be rendered');
$this->assertStringContainsString('<input', $result, 'An input should be rendered');
$this->assertStringContainsString('disabled', $result, 'The input should be disabled');
$this->assertStringContainsString('€5.00', $result, 'The value should be rendered');
}
}

View File

@ -21,9 +21,9 @@ class CurrencyFieldReadonlyTest extends SapphireTest
$field = new CurrencyField_Readonly('Test', '', '$5.00');
$result = $field->Field();
$this->assertContains('<input', $result, 'An input should be rendered');
$this->assertContains('readonly', $result, 'The input should be readonly');
$this->assertContains('$5.00', $result, 'The value should be rendered');
$this->assertStringContainsString('<input', $result, 'An input should be rendered');
$this->assertStringContainsString('readonly', $result, 'The input should be readonly');
$this->assertStringContainsString('$5.00', $result, 'The value should be rendered');
}
public function testFieldWithOutValue()
@ -32,9 +32,9 @@ class CurrencyFieldReadonlyTest extends SapphireTest
$field = new CurrencyField_Readonly('Test', '', null);
$result = $field->Field();
$this->assertContains('<input', $result, 'An input should be rendered');
$this->assertContains('readonly', $result, 'The input should be readonly');
$this->assertContains('AUD0.00', $result, 'The value should be rendered');
$this->assertStringContainsString('<input', $result, 'An input should be rendered');
$this->assertStringContainsString('readonly', $result, 'The input should be readonly');
$this->assertStringContainsString('AUD0.00', $result, 'The value should be rendered');
}
/**
@ -46,8 +46,8 @@ class CurrencyFieldReadonlyTest extends SapphireTest
$field = new CurrencyField_Readonly('Test', '', '€5.00');
$result = $field->Field();
$this->assertContains('<input', $result, 'An input should be rendered');
$this->assertContains('readonly', $result, 'The input should be readonly');
$this->assertContains('€5.00', $result, 'The value should be rendered');
$this->assertStringContainsString('<input', $result, 'An input should be rendered');
$this->assertStringContainsString('readonly', $result, 'The input should be readonly');
$this->assertStringContainsString('€5.00', $result, 'The value should be rendered');
}
}

View File

@ -307,6 +307,6 @@ class CurrencyFieldTest extends SapphireTest
$this->assertFalse($result, 'Validation should fail since wrong currency was used');
$this->assertFalse($validator->getResult()->isValid(), 'Validator should receive failed state');
$this->assertContains('Please enter a valid currency', $validator->getResult()->serialize());
$this->assertStringContainsString('Please enter a valid currency', $validator->getResult()->serialize());
}
}

View File

@ -2,7 +2,7 @@
namespace SilverStripe\Forms;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use SilverStripe\Dev\SapphireTest;
class DatalessFieldTest extends SapphireTest
@ -16,7 +16,7 @@ class DatalessFieldTest extends SapphireTest
public function testFieldHolderAndSmallFieldHolderReturnField()
{
/** @var DatalessField|PHPUnit_Framework_MockObject_MockObject $mock */
/** @var DatalessField|MockObject $mock */
$mock = $this->getMockBuilder(DatalessField::class)
->disableOriginalConstructor()
->setMethods(['Field'])

View File

@ -12,7 +12,7 @@ use SilverStripe\ORM\FieldType\DBDatetime;
*/
class DateFieldDisabledTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
i18n::set_locale('en_NZ');
@ -79,7 +79,7 @@ class DateFieldDisabledTest extends SapphireTest
{
$field = new DateField_Disabled('Test');
$result = $field->Type();
$this->assertContains('readonly', $result, 'Disabled field should be treated as readonly');
$this->assertContains('date_disabled', $result, 'Field should contain date_disabled class');
$this->assertStringContainsString('readonly', $result, 'Disabled field should be treated as readonly');
$this->assertStringContainsString('date_disabled', $result, 'Field should contain date_disabled class');
}
}

View File

@ -3,6 +3,7 @@
namespace SilverStripe\Forms\Tests;
use IntlDateFormatter;
use LogicException;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\DateField;
use SilverStripe\Forms\DateField_Disabled;
@ -16,7 +17,7 @@ use SilverStripe\ORM\FieldType\DBDatetime;
*/
class DateFieldTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
i18n::set_locale('en_NZ');
@ -192,36 +193,30 @@ class DateFieldTest extends SapphireTest
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessageRegExp /Please opt-out .* if using setDateFormat/
*/
public function testHtml5WithCustomFormatThrowsException()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('/Please opt-out .* if using setDateFormat/');
$dateField = new DateField('Date', 'Date');
$dateField->setValue('2010-03-31');
$dateField->setDateFormat('d/M/y');
$dateField->Value();
}
/**
* @expectedException \LogicException
* @expectedExceptionMessageRegExp /Please opt-out .* if using setDateLength/
*/
public function testHtml5WithCustomDateLengthThrowsException()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('/Please opt-out .* if using setDateLength/');
$dateField = new DateField('Date', 'Date');
$dateField->setValue('2010-03-31');
$dateField->setDateLength(IntlDateFormatter::MEDIUM);
$dateField->Value();
}
/**
* @expectedException \LogicException
* @expectedExceptionMessageRegExp /Please opt-out .* if using setLocale/
*/
public function testHtml5WithCustomLocaleThrowsException()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('/Please opt-out .* if using setLocale/');
$dateField = new DateField('Date', 'Date');
$dateField->setValue('2010-03-31');
$dateField->setLocale('de_DE');

View File

@ -17,7 +17,7 @@ class DatetimeFieldTest extends SapphireTest
{
protected $timezone = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
i18n::set_locale('en_NZ');
@ -26,7 +26,7 @@ class DatetimeFieldTest extends SapphireTest
$this->timezone = date_default_timezone_get();
}
protected function tearDown()
protected function tearDown(): void
{
DBDatetime::clear_mock_now();
date_default_timezone_set($this->timezone);
@ -146,7 +146,7 @@ class DatetimeFieldTest extends SapphireTest
$this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00');
// Some localisation packages exclude the ',' in default medium format
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'#29/03/2003(,)? 11:00:00 (PM|pm)#',
$datetimeField->Value(),
'User value is formatted, and in user timezone'
@ -490,12 +490,10 @@ class DatetimeFieldTest extends SapphireTest
$this->assertTrue($result->isReadonly());
}
/**
* @expectedException \BadMethodCallException
* @expectedExceptionMessage Can't change timezone after setting a value
*/
public function testSetTimezoneThrowsExceptionWhenChangingTimezoneAfterSettingValue()
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage("Can't change timezone after setting a value");
date_default_timezone_set('Europe/Berlin');
$field = new DatetimeField('Datetime', 'Time', '2003-03-29 23:59:38');
$field->setTimezone('Pacific/Auckland');

View File

@ -9,12 +9,10 @@ use SilverStripe\ORM\DataObject;
class DefaultFormFactoryTest extends SapphireTest
{
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessageRegExp /Missing required context/
*/
public function testGetFormThrowsExceptionOnMissingContext()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/Missing required context/');
$factory = new DefaultFormFactory();
$factory->getForm();
}

View File

@ -5,7 +5,7 @@ namespace SilverStripe\Forms\Tests;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\EmailField;
use Exception;
use PHPUnit_Framework_AssertionFailedError;
use PHPUnit\Framework\AssertionFailedError;
use SilverStripe\Forms\Tests\EmailFieldTest\TestValidator;
/**
@ -51,7 +51,7 @@ class EmailFieldTest extends FunctionalTest
// If we expect failure and processing gets here without an exception, the test failed
$this->assertTrue($expectSuccess, $checkText . " (/$email/ passed validation, but not expected to)");
} catch (Exception $e) {
if ($e instanceof PHPUnit_Framework_AssertionFailedError) {
if ($e instanceof AssertionFailedError) {
// re-throw assertion failure
throw $e;
} elseif ($expectSuccess) {
@ -77,6 +77,6 @@ class EmailFieldTest extends FunctionalTest
['Email' => 'test@test.com']
);
$this->assertContains('Test save was successful', $response->getBody());
$this->assertStringContainsString('Test save was successful', $response->getBody());
}
}

View File

@ -11,10 +11,10 @@ class FormActionTest extends SapphireTest
public function testGetField()
{
$formAction = new FormAction('test');
$this->assertContains('type="submit"', $formAction->getAttributesHTML());
$this->assertStringContainsString('type="submit"', $formAction->getAttributesHTML());
$formAction->setAttribute('src', 'file.png');
$this->assertContains('type="image"', $formAction->getAttributesHTML());
$this->assertStringContainsString('type="image"', $formAction->getAttributesHTML());
}
public function testGetTitle()

View File

@ -28,7 +28,7 @@ class FormFactoryTest extends SapphireTest
return [];
}
public function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -2,10 +2,10 @@
namespace SilverStripe\Forms\Tests;
use LogicException;
use ReflectionClass;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
@ -40,7 +40,7 @@ class FormFieldTest extends SapphireTest
$field = new FormField('MyField');
$this->assertContains('class1', $field->extraClass(), 'Class list does not contain expected class');
$this->assertStringContainsString('class1', $field->extraClass(), 'Class list does not contain expected class');
FormField::config()->update(
'default_classes',
@ -52,7 +52,7 @@ class FormFieldTest extends SapphireTest
$field = new FormField('MyField');
$this->assertContains('class1 class2', $field->extraClass(), 'Class list does not contain expected class');
$this->assertStringContainsString('class1 class2', $field->extraClass(), 'Class list does not contain expected class');
FormField::config()->update(
'default_classes',
@ -63,11 +63,11 @@ class FormFieldTest extends SapphireTest
$field = new FormField('MyField');
$this->assertContains('class3', $field->extraClass(), 'Class list does not contain expected class');
$this->assertStringContainsString('class3', $field->extraClass(), 'Class list does not contain expected class');
$field->removeExtraClass('class3');
$this->assertNotContains('class3', $field->extraClass(), 'Class list contains unexpected class');
$this->assertStringNotContainsString('class3', $field->extraClass(), 'Class list contains unexpected class');
TextField::config()->update(
'default_classes',
@ -79,8 +79,8 @@ class FormFieldTest extends SapphireTest
$field = new TextField('MyField');
//check default classes inherit
$this->assertContains('class3', $field->extraClass(), 'Class list does not contain inherited class');
$this->assertContains('textfield-class', $field->extraClass(), 'Class list does not contain expected class');
$this->assertStringContainsString('class3', $field->extraClass(), 'Class list does not contain inherited class');
$this->assertStringContainsString('textfield-class', $field->extraClass(), 'Class list does not contain expected class');
Config::unnest();
}
@ -159,35 +159,35 @@ class FormFieldTest extends SapphireTest
$field = new FormField('MyField');
$field->setAttribute('foo', 'bar');
$this->assertContains('foo="bar"', $field->getAttributesHTML());
$this->assertStringContainsString('foo="bar"', $field->getAttributesHTML());
$field->setAttribute('foo', null);
$this->assertNotContains('foo=', $field->getAttributesHTML());
$this->assertStringNotContainsString('foo=', $field->getAttributesHTML());
$field->setAttribute('foo', '');
$this->assertNotContains('foo=', $field->getAttributesHTML());
$this->assertStringNotContainsString('foo=', $field->getAttributesHTML());
$field->setAttribute('foo', false);
$this->assertNotContains('foo=', $field->getAttributesHTML());
$this->assertStringNotContainsString('foo=', $field->getAttributesHTML());
$field->setAttribute('foo', true);
$this->assertContains('foo="foo"', $field->getAttributesHTML());
$this->assertStringContainsString('foo="foo"', $field->getAttributesHTML());
$field->setAttribute('foo', 'false');
$this->assertContains('foo="false"', $field->getAttributesHTML());
$this->assertStringContainsString('foo="false"', $field->getAttributesHTML());
$field->setAttribute('foo', 'true');
$this->assertContains('foo="true"', $field->getAttributesHTML());
$this->assertStringContainsString('foo="true"', $field->getAttributesHTML());
$field->setAttribute('foo', 0);
$this->assertContains('foo="0"', $field->getAttributesHTML());
$this->assertStringContainsString('foo="0"', $field->getAttributesHTML());
$field->setAttribute('one', 1);
$field->setAttribute('two', 2);
$field->setAttribute('three', 3);
$this->assertNotContains('one="1"', $field->getAttributesHTML('one', 'two'));
$this->assertNotContains('two="2"', $field->getAttributesHTML('one', 'two'));
$this->assertContains('three="3"', $field->getAttributesHTML('one', 'two'));
$this->assertStringNotContainsString('one="1"', $field->getAttributesHTML('one', 'two'));
$this->assertStringNotContainsString('two="2"', $field->getAttributesHTML('one', 'two'));
$this->assertStringContainsString('three="3"', $field->getAttributesHTML('one', 'two'));
}
/**
@ -241,18 +241,18 @@ class FormFieldTest extends SapphireTest
{
$field = new FormField('MyField');
$field->setReadonly(true);
$this->assertContains('readonly="readonly"', $field->getAttributesHTML());
$this->assertStringContainsString('readonly="readonly"', $field->getAttributesHTML());
$field->setReadonly(false);
$this->assertNotContains('readonly="readonly"', $field->getAttributesHTML());
$this->assertStringNotContainsString('readonly="readonly"', $field->getAttributesHTML());
}
public function testDisabled()
{
$field = new FormField('MyField');
$field->setDisabled(true);
$this->assertContains('disabled="disabled"', $field->getAttributesHTML());
$this->assertStringContainsString('disabled="disabled"', $field->getAttributesHTML());
$field->setDisabled(false);
$this->assertNotContains('disabled="disabled"', $field->getAttributesHTML());
$this->assertStringNotContainsString('disabled="disabled"', $field->getAttributesHTML());
}
public function testEveryFieldTransformsReadonlyAsClone()
@ -358,7 +358,7 @@ class FormFieldTest extends SapphireTest
{
$field = new FormField('MyField');
$schema = $field->getSchemaDataDefaults();
$this->assertInternalType('array', $schema);
$this->assertIsArray($schema);
}
public function testGetSchemaDataDefaultsTitleTip()
@ -454,11 +454,9 @@ class FormFieldTest extends SapphireTest
$this->assertSame('foo/field/Test/bar', $field->Link('bar'));
}
/**
* @expectedException \LogicException
*/
public function testLinkWithoutForm()
{
$this->expectException(LogicException::class);
$field = new FormField('Test');
$field->Link('bar');
}

View File

@ -20,7 +20,7 @@ use SilverStripe\Forms\PopoverField;
*/
class FormSchemaTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -38,7 +38,7 @@ class FormSchemaTest extends SapphireTest
$expected = json_decode(file_get_contents(__DIR__ . '/FormSchemaTest/testGetSchema.json'), true);
$schema = $formSchema->getSchema($form);
$this->assertInternalType('array', $schema);
$this->assertIsArray($schema);
$this->assertEquals($expected, $schema);
}
@ -62,7 +62,7 @@ class FormSchemaTest extends SapphireTest
];
$state = $formSchema->getState($form);
$this->assertInternalType('array', $state);
$this->assertIsArray($state);
$this->assertEquals($expected, $state);
}
@ -92,7 +92,7 @@ class FormSchemaTest extends SapphireTest
];
$state = $formSchema->getState($form);
$this->assertInternalType('array', $state);
$this->assertIsArray($state);
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
}
@ -136,7 +136,7 @@ class FormSchemaTest extends SapphireTest
];
$state = $formSchema->getState($form);
$this->assertInternalType('array', $state);
$this->assertIsArray($state);
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
}
@ -167,7 +167,7 @@ class FormSchemaTest extends SapphireTest
$expected = json_decode(file_get_contents(__DIR__ . '/FormSchemaTest/testGetNestedSchema.json'), true);
$schema = $formSchema->getSchema($form);
$this->assertInternalType('array', $schema);
$this->assertIsArray($schema);
$this->assertEquals($expected, $schema);
}
@ -207,7 +207,7 @@ class FormSchemaTest extends SapphireTest
$formSchema = new FormSchema();
$schema = $formSchema->getSchema($form);
$expected = json_decode(file_get_contents(__DIR__ . '/FormSchemaTest/testSchemaValidation.json'), true);
$this->assertInternalType('array', $schema);
$this->assertIsArray($schema);
$this->assertEquals($expected, $schema);
}
}

View File

@ -56,7 +56,7 @@ class FormTest extends FunctionalTest
protected static $disable_themes = true;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -140,16 +140,16 @@ class FormTest extends FunctionalTest
);
// Number field updates its value
$this->assertContains('<input type="text" name="Number" value="888"', $response->getBody());
$this->assertStringContainsString('<input type="text" name="Number" value="888"', $response->getBody());
// Readonly field remains
$this->assertContains(
$this->assertStringContainsString(
'<input type="text" name="ReadonlyField" value="This value is readonly"',
$response->getBody()
);
$this->assertNotContains('hacxzored', $response->getBody());
$this->assertStringNotContainsString('hacxzored', $response->getBody());
}
public function testLoadDataFromUnchangedHandling()
@ -502,12 +502,12 @@ class FormTest extends FunctionalTest
'Required fields show a notification on field when left blank'
);
$this->assertContains(
$this->assertStringContainsString(
'&#039;&lt;a href=&quot;http://mysite.com&quot;&gt;link&lt;/a&gt;&#039; is not a number, only numbers can be accepted for this field',
$response->getBody(),
"Validation messages are safely XML encoded"
);
$this->assertNotContains(
$this->assertStringNotContainsString(
'<a href="http://mysite.com">link</a>',
$response->getBody(),
"Unsafe content is not emitted directly inside the response body"
@ -788,7 +788,7 @@ class FormTest extends FunctionalTest
$form = $this->getStubForm();
$this->assertContains('class1', $form->extraClass(), 'Class list does not contain expected class');
$this->assertStringContainsString('class1', $form->extraClass(), 'Class list does not contain expected class');
Form::config()->update(
'default_classes',
@ -800,7 +800,7 @@ class FormTest extends FunctionalTest
$form = $this->getStubForm();
$this->assertContains('class1 class2', $form->extraClass(), 'Class list does not contain expected class');
$this->assertStringContainsString('class1 class2', $form->extraClass(), 'Class list does not contain expected class');
Form::config()->update(
'default_classes',
@ -811,11 +811,11 @@ class FormTest extends FunctionalTest
$form = $this->getStubForm();
$this->assertContains('class3', $form->extraClass(), 'Class list does not contain expected class');
$this->assertStringContainsString('class3', $form->extraClass(), 'Class list does not contain expected class');
$form->removeExtraClass('class3');
$this->assertNotContains('class3', $form->extraClass(), 'Class list contains unexpected class');
$this->assertStringNotContainsString('class3', $form->extraClass(), 'Class list contains unexpected class');
}
public function testAttributes()
@ -903,22 +903,22 @@ class FormTest extends FunctionalTest
$form = $this->getStubForm();
$form->setAttribute('foo', 'bar');
$this->assertContains('foo="bar"', $form->getAttributesHTML());
$this->assertStringContainsString('foo="bar"', $form->getAttributesHTML());
$form->setAttribute('foo', null);
$this->assertNotContains('foo="bar"', $form->getAttributesHTML());
$this->assertStringNotContainsString('foo="bar"', $form->getAttributesHTML());
$form->setAttribute('foo', true);
$this->assertContains('foo="foo"', $form->getAttributesHTML());
$this->assertStringContainsString('foo="foo"', $form->getAttributesHTML());
$form->setAttribute('one', 1);
$form->setAttribute('two', 2);
$form->setAttribute('three', 3);
$form->setAttribute('<html>', '<html>');
$this->assertNotContains('one="1"', $form->getAttributesHTML('one', 'two'));
$this->assertNotContains('two="2"', $form->getAttributesHTML('one', 'two'));
$this->assertContains('three="3"', $form->getAttributesHTML('one', 'two'));
$this->assertNotContains('<html>', $form->getAttributesHTML());
$this->assertStringNotContainsString('one="1"', $form->getAttributesHTML('one', 'two'));
$this->assertStringNotContainsString('two="2"', $form->getAttributesHTML('one', 'two'));
$this->assertStringContainsString('three="3"', $form->getAttributesHTML('one', 'two'));
$this->assertStringNotContainsString('<html>', $form->getAttributesHTML());
}
function testMessageEscapeHtml()
@ -927,7 +927,7 @@ class FormTest extends FunctionalTest
$form->setMessage('<em>Escaped HTML</em>', 'good', ValidationResult::CAST_TEXT);
$parser = new CSSContentParser($form->forTemplate());
$messageEls = $parser->getBySelector('.message');
$this->assertContains(
$this->assertStringContainsString(
'&lt;em&gt;Escaped HTML&lt;/em&gt;',
$messageEls[0]->asXML()
);
@ -936,7 +936,7 @@ class FormTest extends FunctionalTest
$form->setMessage('<em>Unescaped HTML</em>', 'good', ValidationResult::CAST_HTML);
$parser = new CSSContentParser($form->forTemplate());
$messageEls = $parser->getBySelector('.message');
$this->assertContains(
$this->assertStringContainsString(
'<em>Unescaped HTML</em>',
$messageEls[0]->asXML()
);
@ -948,7 +948,7 @@ class FormTest extends FunctionalTest
$form->Fields()->dataFieldByName('key1')->setMessage('<em>Escaped HTML</em>', 'good');
$parser = new CSSContentParser($result = $form->forTemplate());
$messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message');
$this->assertContains(
$this->assertStringContainsString(
'&lt;em&gt;Escaped HTML&lt;/em&gt;',
$messageEls[0]->asXML()
);
@ -961,7 +961,7 @@ class FormTest extends FunctionalTest
->setMessage('<em>Unescaped HTML</em>', 'good', ValidationResult::CAST_HTML);
$parser = new CSSContentParser($form->forTemplate());
$messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message');
$this->assertContains(
$this->assertStringContainsString(
'<em>Unescaped HTML</em>',
$messageEls[0]->asXML()
);
@ -1055,25 +1055,25 @@ class FormTest extends FunctionalTest
// Test our reloaded form field
$body = $response->getBody();
$this->assertContains(
$this->assertStringContainsString(
'<input type="text" name="SomeDateField" value="15/06/2018"',
$body,
'Our reloaded form should contain a SomeDateField with the value "15/06/2018"'
);
$this->assertContains(
$this->assertStringContainsString(
'<input type="text" name="SomeFrenchNumericField" value="9 876,5432" ',
$this->clean($body),
'Our reloaded form should contain a SomeFrenchNumericField with the value "9 876,5432"'
);
$this->assertContains(
$this->assertStringContainsString(
'<input type="text" name="SomeFrenchMoneyField[Currency]" value="NZD"',
$body,
'Our reloaded form should contain a SomeFrenchMoneyField[Currency] with the value "NZD"'
);
$this->assertContains(
$this->assertStringContainsString(
'<input type="text" name="SomeFrenchMoneyField[Amount]" value="9 876,54" ',
$this->clean($body),
'Our reloaded form should contain a SomeFrenchMoneyField[Amount] with the value "9 876,54"'

View File

@ -55,7 +55,7 @@ class GridFieldActionMenuTest extends SapphireTest
Permissions::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->list = new DataList(Team::class);

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Forms\Tests\GridField;
use InvalidArgumentException;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Security\Member;
use SilverStripe\Dev\SapphireTest;
@ -41,11 +42,10 @@ class GridFieldDataColumnsTest extends SapphireTest
/**
* @covers \SilverStripe\Forms\GridField\GridFieldDataColumns::setDisplayFields
* @covers \SilverStripe\Forms\GridField\GridFieldDataColumns::getDisplayFields
*
* @expectedException \InvalidArgumentException
*/
public function testGridFieldDisplayFieldsWithBadArguments()
{
$this->expectException(InvalidArgumentException::class);
$obj = new GridField('testfield', 'testfield', Member::get());
$columns = $obj->getConfig()->getComponentByType(GridFieldDataColumns::class);
$columns->setDisplayFields(new stdClass());

View File

@ -56,7 +56,7 @@ class GridFieldDeleteActionTest extends SapphireTest
Permissions::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->list = new DataList(Team::class);

View File

@ -52,7 +52,7 @@ class GridFieldEditButtonTest extends SapphireTest
Permissions::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->list = new DataList(Team::class);
@ -100,7 +100,7 @@ class GridFieldEditButtonTest extends SapphireTest
$result = $button->getExtraClass();
foreach ($expected as $className) {
$this->assertContains($className, $result);
$this->assertStringContainsString($className, $result);
}
}
@ -109,15 +109,15 @@ class GridFieldEditButtonTest extends SapphireTest
$button = new GridFieldEditButton;
$button->addExtraClass('foobar');
$this->assertContains('foobar', $button->getExtraClass());
$this->assertStringContainsString('foobar', $button->getExtraClass());
$button->removeExtraClass('foobar');
$this->assertNotContains('foobar', $button->getExtraClass());
$this->assertStringNotContainsString('foobar', $button->getExtraClass());
// Check that duplicates are removed
$button->addExtraClass('foobar');
$button->addExtraClass('foobar');
$button->removeExtraClass('foobar');
$this->assertNotContains('foobar', $button->getExtraClass());
$this->assertStringNotContainsString('foobar', $button->getExtraClass());
}
}

View File

@ -35,7 +35,7 @@ class GridFieldExportButtonTest extends SapphireTest
NoView::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -50,7 +50,7 @@ class GridFieldFilterHeaderTest extends SapphireTest
Mom::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->list = new DataList(Team::class);
@ -70,8 +70,8 @@ class GridFieldFilterHeaderTest extends SapphireTest
$htmlFragment = $this->component->getHTMLFragments($this->gridField);
// Check that the output is the new search field
$this->assertContains('<div class="search-holder grid-field__search-holder grid-field__search-holder--hidden"', $htmlFragment['before']);
$this->assertContains('Open search and filter', $htmlFragment['buttons-before-right']);
$this->assertStringContainsString('<div class="search-holder grid-field__search-holder grid-field__search-holder--hidden"', $htmlFragment['before']);
$this->assertStringContainsString('Open search and filter', $htmlFragment['buttons-before-right']);
$this->gridField->getConfig()->removeComponentsByType(GridFieldFilterHeader::class);
$this->gridField->getConfig()->addComponent(new GridFieldFilterHeader(true));
@ -79,7 +79,7 @@ class GridFieldFilterHeaderTest extends SapphireTest
$htmlFragment = $this->component->getHTMLFragments($this->gridField);
// Check that the output is the legacy filter header
$this->assertContains(
$this->assertStringContainsString(
'<tr class="grid-field__filter-header grid-field__search-holder--hidden">',
$htmlFragment['header']
);

View File

@ -44,7 +44,7 @@ class GridFieldLazyLoaderTest extends SapphireTest
Team::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->list = DataList::create(Team::class);
@ -89,8 +89,8 @@ class GridFieldLazyLoaderTest extends SapphireTest
$gridField = $this->getHeaderlessGridField();
$actual = $this->component->getHTMLFragments($gridField);
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
$this->assertContains('grid-field--lazy-loadable', $gridField->extraClass());
$this->assertNotContains('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertStringContainsString('grid-field--lazy-loadable', $gridField->extraClass());
$this->assertStringNotContainsString('grid-field--lazy-loaded', $gridField->extraClass());
}
public function testGetHTMLFragmentsWithoutTabSet()
@ -98,8 +98,8 @@ class GridFieldLazyLoaderTest extends SapphireTest
$gridField = $this->getOutOfTabSetGridField();
$actual = $this->component->getHTMLFragments($gridField);
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
$this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
$this->assertStringContainsString('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertStringNotContainsString('grid-field--lazy-loadable', $gridField->extraClass());
}
public function testGetHTMLFragmentsNonLazy()
@ -107,8 +107,8 @@ class GridFieldLazyLoaderTest extends SapphireTest
$gridField = $this->getNonLazyGridField();
$actual = $this->component->getHTMLFragments($gridField);
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
$this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
$this->assertStringContainsString('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertStringNotContainsString('grid-field--lazy-loadable', $gridField->extraClass());
}
@ -150,8 +150,8 @@ class GridFieldLazyLoaderTest extends SapphireTest
$gridField = $this->makeGridFieldReadonly($this->getHeaderlessGridField());
$actual = $this->component->getHTMLFragments($gridField);
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
$this->assertContains('grid-field--lazy-loadable', $gridField->extraClass());
$this->assertNotContains('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertStringContainsString('grid-field--lazy-loadable', $gridField->extraClass());
$this->assertStringNotContainsString('grid-field--lazy-loaded', $gridField->extraClass());
}
public function testReadOnlyGetHTMLFragmentsWithoutTabSet()
@ -159,8 +159,8 @@ class GridFieldLazyLoaderTest extends SapphireTest
$gridField = $this->makeGridFieldReadonly($this->getOutOfTabSetGridField());
$actual = $this->component->getHTMLFragments($gridField);
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
$this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
$this->assertStringContainsString('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertStringNotContainsString('grid-field--lazy-loadable', $gridField->extraClass());
}
public function testReadOnlyGetHTMLFragmentsNonLazy()
@ -168,8 +168,8 @@ class GridFieldLazyLoaderTest extends SapphireTest
$gridField = $this->makeGridFieldReadonly($this->getNonLazyGridField());
$actual = $this->component->getHTMLFragments($gridField);
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
$this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
$this->assertStringContainsString('grid-field--lazy-loaded', $gridField->extraClass());
$this->assertStringNotContainsString('grid-field--lazy-loadable', $gridField->extraClass());
}
/**

View File

@ -49,7 +49,7 @@ class GridFieldPaginatorTest extends FunctionalTest
Cheerleader::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->list = new DataList(Team::class);

View File

@ -19,7 +19,7 @@ class GridFieldPrintButtonTest extends SapphireTest
TestObject::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -47,23 +47,23 @@ class GridFieldSortableHeaderTest extends SapphireTest
$htmlFragment = $compontent->getHTMLFragments($gridField);
// Check that the output shows name and hat as sortable fields, but not city
$this->assertContains('<span class="non-sortable">City</span>', $htmlFragment['header']);
$this->assertContains(
$this->assertStringContainsString('<span class="non-sortable">City</span>', $htmlFragment['header']);
$this->assertStringContainsString(
'value="Name" class="action grid-field__sort" id="action_SetOrderName"',
$htmlFragment['header']
);
$this->assertContains(
$this->assertStringContainsString(
'value="Cheerleader Hat" class="action grid-field__sort" id="action_SetOrderCheerleader-Hat-Colour"',
$htmlFragment['header']
);
// Check inverse of above
$this->assertNotContains(
$this->assertStringNotContainsString(
'value="City" class="action grid-field__sort" id="action_SetOrderCity"',
$htmlFragment['header']
);
$this->assertNotContains('<span class="non-sortable">Name</span>', $htmlFragment['header']);
$this->assertNotContains('<span class="non-sortable">Cheerleader Hat</span>', $htmlFragment['header']);
$this->assertStringNotContainsString('<span class="non-sortable">Name</span>', $htmlFragment['header']);
$this->assertStringNotContainsString('<span class="non-sortable">Cheerleader Hat</span>', $htmlFragment['header']);
}
public function testGetManipulatedData()
@ -145,20 +145,20 @@ class GridFieldSortableHeaderTest extends SapphireTest
$relationListAsql = Convert::nl2os($relationListA->sql(), ' ');
// Assert that all tables are joined properly
$this->assertContains('FROM "GridFieldSortableHeaderTest_Team"', $relationListAsql);
$this->assertContains(
$this->assertStringContainsString('FROM "GridFieldSortableHeaderTest_Team"', $relationListAsql);
$this->assertStringContainsString(
'LEFT JOIN "GridFieldSortableHeaderTest_TeamGroup" '
. 'ON "GridFieldSortableHeaderTest_TeamGroup"."ID" = "GridFieldSortableHeaderTest_Team"."ID"',
$relationListAsql
);
$this->assertContains(
$this->assertStringContainsString(
'LEFT JOIN "GridFieldSortableHeaderTest_Cheerleader" '
. 'AS "cheerleader_GridFieldSortableHeaderTest_Cheerleader" '
. 'ON "cheerleader_GridFieldSortableHeaderTest_Cheerleader"."ID" = '
. '"GridFieldSortableHeaderTest_Team"."CheerleaderID"',
$relationListAsql
);
$this->assertContains(
$this->assertStringContainsString(
'LEFT JOIN "GridFieldSortableHeaderTest_CheerleaderHat" '
. 'AS "cheerleader_hat_GridFieldSortableHeaderTest_CheerleaderHat" '
. 'ON "cheerleader_hat_GridFieldSortableHeaderTest_CheerleaderHat"."ID" = '
@ -185,15 +185,15 @@ class GridFieldSortableHeaderTest extends SapphireTest
$relationListBsql = $relationListB->sql();
// Assert that subclasses are included in the query
$this->assertContains('FROM "GridFieldSortableHeaderTest_Team"', $relationListBsql);
$this->assertContains(
$this->assertStringContainsString('FROM "GridFieldSortableHeaderTest_Team"', $relationListBsql);
$this->assertStringContainsString(
'LEFT JOIN "GridFieldSortableHeaderTest_TeamGroup" '
. 'ON "GridFieldSortableHeaderTest_TeamGroup"."ID" = "GridFieldSortableHeaderTest_Team"."ID"',
$relationListBsql
);
// Joined tables are joined basetable first
// Note: CheerLeader is base of Mom table, hence the alias
$this->assertContains(
$this->assertStringContainsString(
'LEFT JOIN "GridFieldSortableHeaderTest_Cheerleader" '
. 'AS "cheerleadersmom_GridFieldSortableHeaderTest_Cheerleader" '
. 'ON "cheerleadersmom_GridFieldSortableHeaderTest_Cheerleader"."ID" = '
@ -201,14 +201,14 @@ class GridFieldSortableHeaderTest extends SapphireTest
$relationListBsql
);
// Then the basetable of the joined record is joined to the specific subtable
$this->assertContains(
$this->assertStringContainsString(
'LEFT JOIN "GridFieldSortableHeaderTest_Mom" '
. 'AS "cheerleadersmom_GridFieldSortableHeaderTest_Mom" '
. 'ON "cheerleadersmom_GridFieldSortableHeaderTest_Cheerleader"."ID" = '
. '"cheerleadersmom_GridFieldSortableHeaderTest_Mom"."ID"',
$relationListBsql
);
$this->assertContains(
$this->assertStringContainsString(
'LEFT JOIN "GridFieldSortableHeaderTest_CheerleaderHat" '
. 'AS "cheerleadersmom_hat_GridFieldSortableHeaderTest_CheerleaderHat" '
. 'ON "cheerleadersmom_hat_GridFieldSortableHeaderTest_CheerleaderHat"."ID" = '

View File

@ -2,6 +2,8 @@
namespace SilverStripe\Forms\Tests\GridField;
use InvalidArgumentException;
use LogicException;
use SilverStripe\Dev\CSSContentParser;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
@ -138,11 +140,10 @@ class GridFieldTest extends SapphireTest
/**
* @covers \SilverStripe\Forms\GridField\GridField::getModelClass
*
* @expectedException \LogicException
*/
public function testGridFieldModelClassThrowsException()
{
$this->expectException(LogicException::class);
$obj = new GridField('testfield', 'testfield', ArrayList::create());
$obj->getModelClass();
}
@ -263,11 +264,10 @@ class GridFieldTest extends SapphireTest
/**
* @skipUpgrade
* @covers \SilverStripe\Forms\GridField\GridField::getColumnContent
*
* @expectedException \InvalidArgumentException
*/
public function testGetColumnContentBadArguments()
{
$this->expectException(InvalidArgumentException::class);
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
@ -310,11 +310,10 @@ class GridFieldTest extends SapphireTest
/**
* @covers \SilverStripe\Forms\GridField\GridField::getColumnAttributes
*
* @expectedException \InvalidArgumentException
*/
public function testGetColumnAttributesBadArguments()
{
$this->expectException(\InvalidArgumentException::class);
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
@ -325,11 +324,9 @@ class GridFieldTest extends SapphireTest
$obj->getColumnAttributes($list->first(), 'Non-existing');
}
/**
* @expectedException \LogicException
*/
public function testGetColumnAttributesBadResponseFromComponent()
{
$this->expectException(\LogicException::class);
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
@ -358,11 +355,10 @@ class GridFieldTest extends SapphireTest
/**
* @covers \SilverStripe\Forms\GridField\GridField::getColumnMetadata
*
* @expectedException \LogicException
*/
public function testGetColumnMetadataBadResponseFromComponent()
{
$this->expectException(\LogicException::class);
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
@ -375,11 +371,10 @@ class GridFieldTest extends SapphireTest
/**
* @covers \SilverStripe\Forms\GridField\GridField::getColumnMetadata
*
* @expectedException \InvalidArgumentException
*/
public function testGetColumnMetadataBadArguments()
{
$this->expectException(\InvalidArgumentException::class);
$list = ArrayList::create();
$config = GridFieldConfig::create()->addComponent(new Component);
$obj = new GridField('testfield', 'testfield', $list, $config);
@ -388,11 +383,10 @@ class GridFieldTest extends SapphireTest
/**
* @covers \SilverStripe\Forms\GridField\GridField::handleAction
*
* @expectedException \InvalidArgumentException
*/
public function testHandleActionBadArgument()
{
$this->expectException(\InvalidArgumentException::class);
$obj = new GridField('testfield', 'testfield');
$obj->handleAlterAction('prft', [], []);
}
@ -485,7 +479,7 @@ class GridFieldTest extends SapphireTest
$field = new GridField('testfield', 'testfield', ArrayList::create(), $config);
$form = new Form(null, 'testform', new FieldList([$field]), new FieldList());
$this->assertContains(
$this->assertStringContainsString(
"<div class=\"right\">rightone\nrighttwo</div><div class=\"left\">left</div>",
$field->FieldHolder()
);
@ -521,7 +515,7 @@ class GridFieldTest extends SapphireTest
$field = new GridField('testfield', 'testfield', ArrayList::create(), $config);
$form = new Form(null, 'testform', new FieldList([$field]), new FieldList());
$this->assertContains(
$this->assertStringContainsString(
"<div>first\n<strong>second</strong></div>",
$field->FieldHolder()
);
@ -529,11 +523,10 @@ class GridFieldTest extends SapphireTest
/**
* Test that circular dependencies throw an exception
*
* @expectedException \LogicException
*/
public function testGridFieldCustomFragmentsCircularDependencyThrowsException()
{
$this->expectException(\LogicException::class);
$config = GridFieldConfig::create()->addComponents(
new HTMLFragments(
[
@ -654,7 +647,7 @@ class GridFieldTest extends SapphireTest
// A form that fails validation should display the validation error in the FieldHolder output.
$form->validationResult();
$gridfieldOutput = $gridField->FieldHolder();
$this->assertContains('<p class="message ' . ValidationResult::TYPE_ERROR . '">error</p>', $gridfieldOutput);
$this->assertStringContainsString('<p class="message ' . ValidationResult::TYPE_ERROR . '">error</p>', $gridfieldOutput);
// Clear validation error from previous assertion.
$validator->removeValidation();
@ -664,6 +657,6 @@ class GridFieldTest extends SapphireTest
$form->setValidator(new RequiredFields());
$form->validationResult();
$gridfieldOutput = $gridField->FieldHolder();
$this->assertNotContains('<p class="message ' . ValidationResult::TYPE_ERROR . '">', $gridfieldOutput);
$this->assertStringNotContainsString('<p class="message ' . ValidationResult::TYPE_ERROR . '">', $gridfieldOutput);
}
}

View File

@ -82,7 +82,7 @@ class GroupedDropdownFieldTest extends SapphireTest
]
);
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/<option value="" selected="selected" >\(Choose A\)<\/option>/',
preg_replace('/\s+/', ' ', (string)$field->Field())
);
@ -103,7 +103,7 @@ class GroupedDropdownFieldTest extends SapphireTest
],
]
);
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/<option value="" selected="selected" >\(Choose B\)<\/option>/',
preg_replace('/\s+/', ' ', (string)$field->Field())
);
@ -124,7 +124,7 @@ class GroupedDropdownFieldTest extends SapphireTest
]
);
$field->setEmptyString('(Choose C)');
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/<option value="" selected="selected" >\(Choose C\)<\/option>/',
preg_replace('/\s+/', ' ', (string)$field->Field())
);
@ -152,14 +152,14 @@ class GroupedDropdownFieldTest extends SapphireTest
// value on first level
$field->setValue("1");
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'#<span class="readonly" id="Test">One</span>\n?<input type="hidden" name="Test" value="1" />#',
(string)$field->performReadonlyTransformation()->Field()
);
// value on first level
$field->setValue("2");
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'#<span class="readonly" id="Test">Two</span>\n?<input type="hidden" name="Test" value="2" />#',
(string)$field->performReadonlyTransformation()->Field()
);

View File

@ -18,7 +18,7 @@ use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
class HTMLEditorConfigTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -140,7 +140,7 @@ class HTMLEditorConfigTest extends SapphireTest
$this->assertNotContains('plugin1', array_keys($plugins));
$this->assertNotContains('plugin2', array_keys($plugins));
}
public function testRequireJSIncludesAllConfigs()
{
$a = HTMLEditorConfig::get('configA');
@ -161,7 +161,7 @@ class HTMLEditorConfigTest extends SapphireTest
try {
$config = new TinyMCEConfig();
$this->expectException(Exception::class);
$this->expectExceptionMessageRegExp('/module is not installed/');
$this->expectExceptionMessageMatches('/module is not installed/');
$config->getScriptURL();
} finally {
ModuleLoader::inst()->popManifest();

View File

@ -25,7 +25,7 @@ class HTMLEditorFieldTest extends FunctionalTest
TestObject::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -55,7 +55,7 @@ class HTMLEditorFieldTest extends FunctionalTest
}
}
protected function tearDown()
protected function tearDown(): void
{
TestAssetStore::reset();
parent::tearDown();
@ -73,12 +73,12 @@ class HTMLEditorFieldTest extends FunctionalTest
$inputText = "These are some unicodes: ä, ö, & ü";
$field = new HTMLEditorField("Test", "Test");
$field->setValue($inputText);
$this->assertContains('These are some unicodes: &auml;, &ouml;, &amp; &uuml;', $field->Field());
$this->assertStringContainsString('These are some unicodes: &auml;, &ouml;, &amp; &uuml;', $field->Field());
// Test shortcodes
$inputText = "Shortcode: [file_link id=4]";
$field = new HTMLEditorField("Test", "Test");
$field->setValue($inputText);
$this->assertContains('Shortcode: [file_link id=4]', $field->Field());
$this->assertStringContainsString('Shortcode: [file_link id=4]', $field->Field());
}
public function testBasicSaving()

View File

@ -13,7 +13,7 @@ use SilverStripe\View\SSViewer;
class TinyMCECombinedGeneratorTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -27,7 +27,7 @@ class TinyMCECombinedGeneratorTest extends SapphireTest
->set('editor_css', [ 'mycode/editor.css' ]);
}
protected function tearDown()
protected function tearDown(): void
{
parent::tearDown();
// Flush test configs
@ -59,36 +59,36 @@ class TinyMCECombinedGeneratorTest extends SapphireTest
// Get config for this
/** @var TinyMCECombinedGenerator $generator */
$generator = Injector::inst()->create(TinyMCECombinedGenerator::class);
$this->assertRegExp('#_tinymce/tinymce-testconfig-[0-9a-z]{10,10}#', $generator->generateFilename($c));
$this->assertMatchesRegularExpression('#_tinymce/tinymce-testconfig-[0-9a-z]{10,10}#', $generator->generateFilename($c));
$content = $generator->generateContent($c);
$this->assertContains(
$this->assertStringContainsString(
"var baseURL = baseTag.length ? baseTag[0].baseURI : 'http://www.mysite.com/basedir/';\n",
$content
);
// Main script file
$this->assertContains("/* tinymce.js */\n", $content);
$this->assertStringContainsString("/* tinymce.js */\n", $content);
// Locale file
$this->assertContains("/* en.js */\n", $content);
$this->assertStringContainsString("/* en.js */\n", $content);
// Local plugins
$this->assertContains("/* plugin1.js */\n", $content);
$this->assertContains("/* plugin4.min.js */\n", $content);
$this->assertContains("/* plugin4/langs/en.js */\n", $content);
$this->assertContains("/* plugin5.js */\n", $content);
$this->assertContains("/* plugin6.js */\n", $content);
$this->assertStringContainsString("/* plugin1.js */\n", $content);
$this->assertStringContainsString("/* plugin4.min.js */\n", $content);
$this->assertStringContainsString("/* plugin4/langs/en.js */\n", $content);
$this->assertStringContainsString("/* plugin5.js */\n", $content);
$this->assertStringContainsString("/* plugin6.js */\n", $content);
// module-resource plugin
$this->assertContains("/* plugin8.js */\n", $content);
$this->assertStringContainsString("/* plugin8.js */\n", $content);
// Exclude non-local plugins
$this->assertNotContains('plugin2.js', $content);
$this->assertNotContains('plugin3.js', $content);
$this->assertStringNotContainsString('plugin2.js', $content);
$this->assertStringNotContainsString('plugin3.js', $content);
// Exclude missing file
$this->assertNotContains('plugin7.js', $content);
$this->assertStringNotContainsString('plugin7.js', $content);
// Check themes
$this->assertContains("/* theme.js */\n", $content);
$this->assertContains("/* testtheme/langs/en.js */\n", $content);
$this->assertStringContainsString("/* theme.js */\n", $content);
$this->assertStringContainsString("/* testtheme/langs/en.js */\n", $content);
// Check plugin links included
$this->assertContains(
$this->assertStringContainsString(
<<<EOS
tinymce.each('tinymce/langs/en.js,mycode/plugin1.js,tinymce/plugins/plugin4/plugin.min.js,tinymce/plugins/plugin4/langs/en.js,tinymce/plugins/plugin5/plugin.js,mycode/plugin6.js,mycode/plugin8.js?m=
EOS
@ -97,7 +97,7 @@ EOS
);
// Check theme links included
$this->assertContains(
$this->assertStringContainsString(
<<<EOS
tinymce/themes/testtheme/theme.js,tinymce/themes/testtheme/langs/en.js'.split(','),function(f){tinymce.ScriptLoader.markDone(baseURL+f);});
EOS

View File

@ -37,7 +37,7 @@ class TinyMCEConfigTest extends SapphireTest
);
// Check we don't simplify to locale when a specific version exists
if (strpos($resource, '_') === false) {
$this->assertFileNotExists(
$this->assertFileDoesNotExist(
"{$langs}/{$locale}.js",
"Locale code {$locale} doesn't map to simple {$resource}.js when a better {$locale}.js is available"
);

View File

@ -18,8 +18,8 @@ class LookupFieldTest extends SapphireTest
$field->setValue(null);
$result = trim($field->Field()->getValue());
$this->assertContains('<span class="readonly" id="test"><i>(none)</i></span>', $result);
$this->assertContains('<input type="hidden" name="test" value="" />', $result);
$this->assertStringContainsString('<span class="readonly" id="test"><i>(none)</i></span>', $result);
$this->assertStringContainsString('<input type="hidden" name="test" value="" />', $result);
}
public function testStringValueWithNumericArraySource()
@ -28,8 +28,8 @@ class LookupFieldTest extends SapphireTest
$field = new LookupField('test', 'test', $source);
$field->setValue(1);
$result = trim($field->Field()->getValue());
$this->assertContains('<span class="readonly" id="test">one</span>', $result);
$this->assertContains('<input type="hidden" name="test" value="1" />', $result);
$this->assertStringContainsString('<span class="readonly" id="test">one</span>', $result);
$this->assertStringContainsString('<input type="hidden" name="test" value="1" />', $result);
}
public function testUnknownStringValueWithNumericArraySource()
@ -39,8 +39,8 @@ class LookupFieldTest extends SapphireTest
$field->setValue('w00t');
$result = trim($field->Field()->getValue());
$this->assertContains('<span class="readonly" id="test">w00t</span>', $result);
$this->assertContains('<input type="hidden" name="test" value="" />', $result);
$this->assertStringContainsString('<span class="readonly" id="test">w00t</span>', $result);
$this->assertStringContainsString('<input type="hidden" name="test" value="" />', $result);
}
public function testArrayValueWithAssociativeArraySource()
@ -51,8 +51,8 @@ class LookupFieldTest extends SapphireTest
$field->setValue(['one','two']);
$result = trim($field->Field()->getValue());
$this->assertContains('<span class="readonly" id="test">one val, two val</span>', $result);
$this->assertContains('<input type="hidden" name="test" value="one, two" />', $result);
$this->assertStringContainsString('<span class="readonly" id="test">one val, two val</span>', $result);
$this->assertStringContainsString('<input type="hidden" name="test" value="one, two" />', $result);
}
public function testArrayValueWithNumericArraySource()
@ -63,8 +63,8 @@ class LookupFieldTest extends SapphireTest
$field->setValue([1,2]);
$result = trim($field->Field()->getValue());
$this->assertContains('<span class="readonly" id="test">one, two</span>', $result);
$this->assertContains('<input type="hidden" name="test" value="1, 2" />', $result);
$this->assertStringContainsString('<span class="readonly" id="test">one, two</span>', $result);
$this->assertStringContainsString('<input type="hidden" name="test" value="1, 2" />', $result);
}
public function testArrayValueWithSqlMapSource()
@ -78,8 +78,8 @@ class LookupFieldTest extends SapphireTest
$field->setValue([$member1->ID, $member2->ID]);
$result = trim($field->Field()->getValue());
$this->assertContains('<span class="readonly" id="test">member1, member2</span>', $result);
$this->assertContains(sprintf(
$this->assertStringContainsString('<span class="readonly" id="test">member1, member2</span>', $result);
$this->assertStringContainsString(sprintf(
'<input type="hidden" name="test" value="%s, %s" />',
$member1->ID,
$member2->ID
@ -104,13 +104,13 @@ class LookupFieldTest extends SapphireTest
$field->setValue(3);
$result = trim($field->Field()->getValue());
$this->assertContains('<span class="readonly" id="test">Carrots</span>', $result);
$this->assertContains('<input type="hidden" name="test" value="3" />', $result);
$this->assertStringContainsString('<span class="readonly" id="test">Carrots</span>', $result);
$this->assertStringContainsString('<input type="hidden" name="test" value="3" />', $result);
$field->setValue([3, 9]);
$result = trim($field->Field()->getValue());
$this->assertContains('<span class="readonly" id="test">Carrots, Vegan</span>', $result);
$this->assertContains('<input type="hidden" name="test" value="3, 9" />', $result);
$this->assertStringContainsString('<span class="readonly" id="test">Carrots, Vegan</span>', $result);
$this->assertStringContainsString('<input type="hidden" name="test" value="3, 9" />', $result);
}
}

View File

@ -119,8 +119,8 @@ class NumericFieldTest extends SapphireTest
$field->setScale(2);
$field->setValue(1001.3);
$html = $field->performReadonlyTransformation()->Field()->forTemplate();
$this->assertContains('value="1.001,30"', $html);
$this->assertContains('readonly="readonly"', $html);
$this->assertStringContainsString('value="1.001,30"', $html);
$this->assertStringContainsString('readonly="readonly"', $html);
}
public function testNumberTypeOnInputHtml()
@ -131,7 +131,7 @@ class NumericFieldTest extends SapphireTest
// @todo - Revert to number one day when html5 number supports proper localisation
// See https://github.com/silverstripe/silverstripe-framework/pull/4565
$this->assertContains('type="text"', $html, 'number type not set');
$this->assertStringContainsString('type="text"', $html, 'number type not set');
}
public function testNullSet()

View File

@ -95,11 +95,11 @@ class OptionsetFieldTest extends SapphireTest
]
);
$fieldHTML = (string)$field1->Field();
$this->assertContains('One', $fieldHTML);
$this->assertContains('Two &amp; Three', $fieldHTML);
$this->assertNotContains('Two & Three', $fieldHTML);
$this->assertContains('Four &amp; Five &amp; Six', $fieldHTML);
$this->assertNotContains('Four & Five & Six', $fieldHTML);
$this->assertStringContainsString('One', $fieldHTML);
$this->assertStringContainsString('Two &amp; Three', $fieldHTML);
$this->assertStringNotContainsString('Two & Three', $fieldHTML);
$this->assertStringContainsString('Four &amp; Five &amp; Six', $fieldHTML);
$this->assertStringNotContainsString('Four & Five & Six', $fieldHTML);
}
/**

View File

@ -25,12 +25,12 @@ class PrintableTransformationTabSetTest extends SapphireTest
$transformationTabSet = new PrintableTransformation_TabSet($tabs);
$result = $transformationTabSet->FieldHolder();
$this->assertContains('<h1>Main</h1>', $result);
$this->assertContains('<h1>Secondary</h1>', $result);
$this->assertStringContainsString('<h1>Main</h1>', $result);
$this->assertStringContainsString('<h1>Secondary</h1>', $result);
$transformationTabSet->setTabSet($optionsTabSet);
$result = $transformationTabSet->FieldHolder();
$this->assertContains('<h2>Options</h2>', $result);
$this->assertStringContainsString('<h2>Options</h2>', $result);
}
}

View File

@ -36,8 +36,8 @@ class SelectionGroupTest extends SapphireTest
$this->assertEquals(' one title', (string)$listElOne->label[0]);
$this->assertEquals(' two title', (string)$listElTwo->label[0]);
$this->assertContains('one view', (string)$listElOne->div);
$this->assertContains('two view', (string)$listElTwo->div);
$this->assertStringContainsString('one view', (string)$listElOne->div);
$this->assertStringContainsString('two view', (string)$listElTwo->div);
}
public function testSelectedFieldHolder()

View File

@ -14,12 +14,12 @@ class TextareaFieldTest extends SapphireTest
$inputText = "These are some unicodes: ä, ö, & ü";
$field = new TextareaField("Test", "Test");
$field->setValue($inputText);
$this->assertContains('These are some unicodes: &auml;, &ouml;, &amp; &uuml;', $field->Field());
$this->assertStringContainsString('These are some unicodes: &auml;, &ouml;, &amp; &uuml;', $field->Field());
// Test shortcodes
$inputText = "Shortcode: [file_link id=4]";
$field = new TextareaField("Test", "Test");
$field->setValue($inputText);
$this->assertContains('Shortcode: [file_link id=4]', $field->Field());
$this->assertStringContainsString('Shortcode: [file_link id=4]', $field->Field());
}
/**
@ -31,7 +31,7 @@ class TextareaFieldTest extends SapphireTest
$field = new TextareaField("Test", "Test");
$field->setValue($inputText);
$field = $field->performReadonlyTransformation();
$this->assertContains('These are some unicodes: äöü', $field->Field());
$this->assertStringContainsString('These are some unicodes: äöü', $field->Field());
}
/**
@ -43,7 +43,7 @@ class TextareaFieldTest extends SapphireTest
$field = new TextareaField("Test", "Test");
$field = $field->performReadonlyTransformation();
$field->setValue($inputText);
$this->assertContains(
$this->assertStringContainsString(
'These are some special &lt;html&gt; chars including &#039;single&#039; &amp;'
. ' &quot;double&quot; quotations',
$field->Field()

View File

@ -9,22 +9,22 @@ use SilverStripe\i18n\i18n;
class TimeFieldReadonlyTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
i18n::set_locale('en_NZ');
}
public function testPerformReadonly()
{
$field = new TimeField('Time', 'Time', '23:00:00');
$roField = $field->performReadonlyTransformation();
$this->assertInstanceOf(TimeField_Readonly::class, $roField);
$this->assertTrue($roField->isReadonly());
$this->assertEquals($roField->dataValue(), '23:00:00');
}
public function testSettingsCarryOver()
{
$field = new TimeField('Time', 'Time');
@ -35,7 +35,7 @@ class TimeFieldReadonlyTest extends SapphireTest
->setLocale('en_US')
->setTimeLength(IntlDateFormatter::SHORT)
->setValue('23:00:00');
$roField = $field->performReadonlyTransformation();
$this->assertFalse($roField->getHTML5());
$this->assertEquals($roField->getTimeFormat(), 'KK:mma');

View File

@ -3,6 +3,7 @@
namespace SilverStripe\Forms\Tests;
use IntlDateFormatter;
use LogicException;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\TimeField;
@ -11,7 +12,7 @@ use SilverStripe\i18n\i18n;
class TimeFieldTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
i18n::set_locale('en_NZ');
@ -147,36 +148,30 @@ class TimeFieldTest extends SapphireTest
$this->assertEquals($f->Value(), '23:59:00');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessageRegExp /Please opt-out .* if using setTimeFormat/
*/
public function testHtml5WithCustomFormatThrowsException()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('/Please opt-out .* if using setTimeFormat/');
$f = new TimeField('Time', 'Time');
$f->setValue('15:59:00');
$f->setTimeFormat('mm:HH');
$f->Value();
}
/**
* @expectedException \LogicException
* @expectedExceptionMessageRegExp /Please opt-out .* if using setTimeLength/
*/
public function testHtml5WithCustomDateLengthThrowsException()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('/Please opt-out .* if using setTimeLength/');
$f = new TimeField('Time', 'Time');
$f->setValue('15:59:00');
$f->setTimeLength(IntlDateFormatter::MEDIUM);
$f->Value();
}
/**
* @expectedException \LogicException
* @expectedExceptionMessageRegExp /Please opt-out .* if using setLocale/
*/
public function testHtml5WithCustomLocaleThrowsException()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('/Please opt-out .* if using setLocale/');
$f = new TimeField('Time', 'Time');
$f->setValue('15:59:00');
$f->setLocale('de_DE');

View File

@ -52,23 +52,21 @@ class TipTest extends SapphireTest
/**
* Ensure passing an invalid importance level to the constructor fails
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Provided importance level must be defined in Tip::IMPORTANCE_LEVELS
*/
public function testInvalidImportanceLevelInConstructorCausesException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Provided importance level must be defined in Tip::IMPORTANCE_LEVELS');
$tip = new Tip('message', 'arbitrary-importance');
}
/**
* Ensure setting an invalid importance level fails
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Provided importance level must be defined in Tip::IMPORTANCE_LEVELS
*/
public function testInvalidImportanceLevelInSetterCausesException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Provided importance level must be defined in Tip::IMPORTANCE_LEVELS');
$tip = new Tip('message');
$tip->setImportanceLevel('arbitrary-importance');

View File

@ -308,11 +308,11 @@ class TreeDropdownFieldTest extends SapphireTest
$field->setValue($fileMock->ID);
$readonlyField = $field->performReadonlyTransformation();
$result = (string) $readonlyField->Field();
$this->assertContains(
$this->assertStringContainsString(
'<span class="readonly" id="TestTree">&lt;Special &amp; characters&gt;</span>',
$result
);
$this->assertContains(
$this->assertStringContainsString(
'<input type="hidden" name="TestTree" value="' . $fileMock->ID . '" />',
$result
);

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Forms\Tests;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use SilverStripe\Assets\File;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\Form;
@ -12,6 +13,8 @@ use SilverStripe\View\SSViewer;
class TreeMultiselectFieldTest extends SapphireTest
{
use ArraySubsetAsserts;
protected static $fixture_file = 'TreeDropdownFieldTest.yml';
protected static $extra_dataobjects = [
@ -56,7 +59,7 @@ class TreeMultiselectFieldTest extends SapphireTest
*/
protected $fieldValue;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -170,8 +173,8 @@ class TreeMultiselectFieldTest extends SapphireTest
$this->assertCount(0, $items, 'there must be no items selected');
$html = $field->Field();
$this->assertContains($field->ID(), $html);
$this->assertContains('unchanged', $html);
$this->assertStringContainsString($field->ID(), $html);
$this->assertStringContainsString('unchanged', $html);
}
@ -199,8 +202,8 @@ class TreeMultiselectFieldTest extends SapphireTest
$this->assertCount(2, $items, 'there must be exactly 2 items selected');
$html = $field->Field();
$this->assertContains($field->ID(), $html);
$this->assertContains($this->fieldValue, $html);
$this->assertStringContainsString($field->ID(), $html);
$this->assertStringContainsString($this->fieldValue, $html);
}
/**
@ -248,7 +251,7 @@ class TreeMultiselectFieldTest extends SapphireTest
$this->assertCount(0, $items, 'there must be 0 selected items');
$html = $field->Field();
$this->assertContains($field->ID(), $html);
$this->assertStringContainsString($field->ID(), $html);
}
/**
@ -298,8 +301,8 @@ class TreeMultiselectFieldTest extends SapphireTest
$this->assertCount(2, $items, 'there must be exactly 2 selected items');
$html = $field->Field();
$this->assertContains($field->ID(), $html);
$this->assertContains($this->fieldValue, $html);
$this->assertStringContainsString($field->ID(), $html);
$this->assertStringContainsString($this->fieldValue, $html);
}
public function testGetItems()

View File

@ -2,7 +2,7 @@
namespace SilverStripe\Logging\Tests;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
@ -11,7 +11,7 @@ use SilverStripe\Logging\DebugViewFriendlyErrorFormatter;
class DebugViewFriendlyErrorFormatterTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Email::config()->set('admin_email', 'testy@mctest.face');
@ -19,7 +19,7 @@ class DebugViewFriendlyErrorFormatterTest extends SapphireTest
public function testFormatPassesRecordCodeToOutput()
{
/** @var DebugViewFriendlyErrorFormatter|PHPUnit_Framework_MockObject_MockObject $mock */
/** @var DebugViewFriendlyErrorFormatter|MockObject $mock */
$mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
->setMethods(['output'])
->getMock();
@ -30,7 +30,7 @@ class DebugViewFriendlyErrorFormatterTest extends SapphireTest
public function testFormatPassesInstanceStatusCodeToOutputWhenNotProvidedByRecord()
{
/** @var DebugViewFriendlyErrorFormatter|PHPUnit_Framework_MockObject_MockObject $mock */
/** @var DebugViewFriendlyErrorFormatter|MockObject $mock */
$mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
->setMethods(['output'])
->getMock();
@ -49,7 +49,7 @@ class DebugViewFriendlyErrorFormatterTest extends SapphireTest
['horse' => 'caballo'],
];
/** @var DebugViewFriendlyErrorFormatter|PHPUnit_Framework_MockObject_MockObject $mock */
/** @var DebugViewFriendlyErrorFormatter|MockObject $mock */
$mock = $this->getMockBuilder(DebugViewFriendlyErrorFormatter::class)
->setMethods(['format'])
->getMock();

View File

@ -19,10 +19,10 @@ class DetailedErrorFormatterTest extends SapphireTest
]]);
$base = __DIR__;
$this->assertContains('ERROR [Emergency]: Uncaught Exception: Error', $output);
$this->assertContains("Line 32 in $base/DetailedErrorFormatterTest/ErrorGenerator.php", $output);
$this->assertContains('* 32: throw new Exception(\'Error\');', $output);
$this->assertContains(
$this->assertStringContainsString('ERROR [Emergency]: Uncaught Exception: Error', $output);
$this->assertStringContainsString("Line 32 in $base/DetailedErrorFormatterTest/ErrorGenerator.php", $output);
$this->assertStringContainsString('* 32: throw new Exception(\'Error\');', $output);
$this->assertStringContainsString(
'SilverStripe\\Logging\\Tests\\DetailedErrorFormatterTest\\ErrorGenerator->mockException(4)',
$output
);
@ -40,10 +40,10 @@ class DetailedErrorFormatterTest extends SapphireTest
$formatter = new DetailedErrorFormatter();
$result = $formatter->format($record);
$this->assertContains('ERRNO 401', $result, 'Status code was not found in trace');
$this->assertContains('Denied', $result, 'Message was not found in trace');
$this->assertContains('Line 4 in index.php', $result, 'Line or filename were not found in trace');
$this->assertContains(self::class, $result, 'Backtrace doesn\'t show current test class');
$this->assertStringContainsString('ERRNO 401', $result, 'Status code was not found in trace');
$this->assertStringContainsString('Denied', $result, 'Message was not found in trace');
$this->assertStringContainsString('Line 4 in index.php', $result, 'Line or filename were not found in trace');
$this->assertStringContainsString(self::class, $result, 'Backtrace doesn\'t show current test class');
}
public function testFormatBatch()
@ -66,9 +66,9 @@ class DetailedErrorFormatterTest extends SapphireTest
$formatter = new DetailedErrorFormatter();
$result = $formatter->formatBatch($records);
$this->assertContains('ERRNO 401', $result, 'First status code was not found in trace');
$this->assertContains('ERRNO 404', $result, 'Second status code was not found in trace');
$this->assertContains('Denied', $result, 'First message was not found in trace');
$this->assertContains('Not found', $result, 'Second message was not found in trace');
$this->assertStringContainsString('ERRNO 401', $result, 'First status code was not found in trace');
$this->assertStringContainsString('ERRNO 404', $result, 'Second status code was not found in trace');
$this->assertStringContainsString('Denied', $result, 'First message was not found in trace');
$this->assertStringContainsString('Not found', $result, 'Second message was not found in trace');
}
}

View File

@ -12,7 +12,7 @@ use SilverStripe\Logging\HTTPOutputHandler;
class HTTPOutputHandlerTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -2,18 +2,17 @@
namespace SilverStripe\Logging\Tests;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Logging\MonologErrorHandler;
class MonologErrorHandlerTest extends SapphireTest
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /No Logger properties passed to MonologErrorHandler/
*/
public function testStartThrowsExceptionWithoutLoggerDefined()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/No Logger properties passed to MonologErrorHandler/');
$handler = new MonologErrorHandler();
$handler->start();
}

View File

@ -133,19 +133,6 @@ class ArrayListTest extends SapphireTest
);
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testZeroLimit()
{
Deprecation::notification_version('4.3.0');
$list = new ArrayList([
['Key' => 1],
['Key' => 2],
]);
$list->limit(0);
}
public function testAddRemove()
{
$list = new ArrayList(
@ -737,6 +724,7 @@ class ArrayListTest extends SapphireTest
// This call will trigger a fatal error if there are issues with circular dependencies
$items->sort('Sort');
$this->assertTrue(true, 'Sort with circular dependencies does not trigger an error.');
}
/**

View File

@ -2,6 +2,8 @@
namespace SilverStripe\ORM\Tests;
use InvalidArgumentException;
use PHPUnit\Framework\Error\Notice;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\FieldType\DBDate;
@ -15,7 +17,7 @@ class DBDateTest extends SapphireTest
{
protected $oldError = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->oldError = error_reporting();
@ -24,7 +26,7 @@ class DBDateTest extends SapphireTest
i18n::set_locale('en_NZ');
}
protected function tearDown()
protected function tearDown(): void
{
$this->restoreNotices();
parent::tearDown();
@ -36,7 +38,6 @@ class DBDateTest extends SapphireTest
protected function suppressNotices()
{
error_reporting(error_reporting() & ~E_USER_NOTICE);
\PHPUnit_Framework_Error_Notice::$enabled = false;
}
/**
@ -45,7 +46,6 @@ class DBDateTest extends SapphireTest
protected function restoreNotices()
{
error_reporting($this->oldError);
\PHPUnit_Framework_Error_Notice::$enabled = true;
}
public function testNiceDate()
@ -92,21 +92,17 @@ class DBDateTest extends SapphireTest
);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid date: '3/16/2003'. Use y-MM-dd to prevent this error.
*/
public function testMDYConversion()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid date: '3/16/2003'. Use y-MM-dd to prevent this error.");
DBField::create_field('Date', '3/16/2003');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid date: '03-03-04'. Use y-MM-dd to prevent this error.
*/
public function testY2kCorrection()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid date: '03-03-04'. Use y-MM-dd to prevent this error.");
DBField::create_field('Date', '03-03-04');
}

View File

@ -11,7 +11,7 @@ use SilverStripe\ORM\FieldType\DBDatetime;
*/
class DBDatetimeTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
i18n::set_locale('en_NZ');
@ -108,10 +108,10 @@ class DBDatetimeTest extends SapphireTest
// note: Some localisation packages exclude the ',' in default medium format
i18n::set_locale('en_NZ');
$this->assertRegExp('#11/12/2001(,)? 10:10 PM#i', $date->Nice());
$this->assertMatchesRegularExpression('#11/12/2001(,)? 10:10 PM#i', $date->Nice());
i18n::set_locale('en_US');
$this->assertRegExp('#Dec 11(,)? 2001(,)? 10:10 PM#i', $date->Nice());
$this->assertMatchesRegularExpression('#Dec 11(,)? 2001(,)? 10:10 PM#i', $date->Nice());
}
public function testDate()
@ -123,7 +123,7 @@ class DBDatetimeTest extends SapphireTest
public function testTime()
{
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
$this->assertRegexp('#10:10:59 PM#i', $date->Time());
$this->assertMatchesRegularExpression('#10:10:59 PM#i', $date->Time());
}
public function testTime24()

View File

@ -17,7 +17,7 @@ class DBHTMLTextTest extends SapphireTest
private $previousLocaleSetting = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -30,7 +30,7 @@ class DBHTMLTextTest extends SapphireTest
ShortcodeParser::set_active('htmltest');
}
protected function tearDown()
protected function tearDown(): void
{
// If a test sets the locale, reset it on teardown

View File

@ -14,14 +14,14 @@ class DBTextTest extends SapphireTest
private $previousLocaleSetting = null;
public function setUp()
protected function setUp(): void
{
parent::setUp();
// clear the previous locale setting
$this->previousLocaleSetting = null;
}
public function tearDown()
protected function tearDown(): void
{
parent::tearDown();
// If a test sets the locale, reset it on teardown

View File

@ -10,7 +10,7 @@ use SilverStripe\Security\Member;
class DBTimeTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
i18n::set_locale('en_NZ');
@ -48,12 +48,12 @@ class DBTimeTest extends SapphireTest
public function testNice()
{
$time = DBTime::create_field('Time', '17:15:55');
$this->assertRegexp('#5:15:55 PM#i', $time->Nice());
$this->assertMatchesRegularExpression('#5:15:55 PM#i', $time->Nice());
}
public function testShort()
{
$time = DBTime::create_field('Time', '17:15:55');
$this->assertRegexp('#5:15 PM#i', $time->Short());
$this->assertMatchesRegularExpression('#5:15 PM#i', $time->Short());
}
}

View File

@ -92,11 +92,9 @@ class DataListTest extends SapphireTest
$this->assertEquals(2, $newList->Count(), 'List should only contain two objects after subtraction');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testSubtractBadDataclassThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$teamsComments = TeamComment::get();
$teams = Team::get();
$teamsComments->subtract($teams);
@ -155,13 +153,13 @@ class DataListTest extends SapphireTest
public function testDistinct()
{
$list = TeamComment::get();
$this->assertContains('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query is set as distinct by default');
$this->assertStringContainsString('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query is set as distinct by default');
$list = $list->distinct(false);
$this->assertNotContains('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query does not contain distinct');
$this->assertStringNotContainsString('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query does not contain distinct');
$list = $list->distinct(true);
$this->assertContains('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query contains distinct');
$this->assertStringContainsString('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query contains distinct');
}
public function testDataClass()
@ -432,8 +430,8 @@ class DataListTest extends SapphireTest
// Assert that filtering on ID searches by the base table, not the child table field
$query = SubTeam::get()->filter('ID', 4)->sql($parameters);
$this->assertContains('WHERE ("DataObjectTest_Team"."ID" = ?)', $query);
$this->assertNotContains('WHERE ("DataObjectTest_SubTeam"."ID" = ?)', $query);
$this->assertStringContainsString('WHERE ("DataObjectTest_Team"."ID" = ?)', $query);
$this->assertStringNotContainsString('WHERE ("DataObjectTest_SubTeam"."ID" = ?)', $query);
}
public function testByIDs()
@ -585,12 +583,10 @@ class DataListTest extends SapphireTest
$this->assertEquals('Phil', $list->last()->Name);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Fans is not a linear relation on model SilverStripe\ORM\Tests\DataObjectTest\Player
*/
public function testSortInvalidParameters()
{
$this->expectException(InvalidArgumentException::class);
$this->expectDeprecationMessage('Fans is not a linear relation on model SilverStripe\ORM\Tests\DataObjectTest\Player');
$list = Team::get();
$list->sort('Founder.Fans.Surname'); // Can't sort on has_many
}
@ -756,7 +752,7 @@ class DataListTest extends SapphireTest
public function testSimpleFilterWithNonExistingComparisator()
{
$this->expectException(InjectorNotFoundException::class);
$this->expectExceptionMessageRegExp('/Class "?DataListFilter.Bogus"? does not exist/');
$this->expectExceptionMessageMatches('/Class "?DataListFilter.Bogus"? does not exist/');
$list = TeamComment::get();
$list->filter('Comment:Bogus', 'team comment');
@ -768,7 +764,7 @@ class DataListTest extends SapphireTest
public function testInvalidModifier()
{
$this->expectException(InjectorNotFoundException::class);
$this->expectExceptionMessageRegExp('/Class "?DataListFilter.invalidmodifier"? does not exist/');
$this->expectExceptionMessageMatches('/Class "?DataListFilter.invalidmodifier"? does not exist/');
$list = TeamComment::get();
$list->filter('Comment:invalidmodifier', 'team comment');
@ -1093,12 +1089,10 @@ class DataListTest extends SapphireTest
$this->assertEquals('007', $list->first()->ShirtNumber);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage MascotAnimal is not a relation on model SilverStripe\ORM\Tests\DataObjectTest\Team
*/
public function testFilterOnInvalidRelation()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('MascotAnimal is not a relation on model SilverStripe\ORM\Tests\DataObjectTest\Team');
// Filter on missing relation 'MascotAnimal'
Team::get()
->filter('MascotAnimal.Name', 'Richard')
@ -1351,7 +1345,7 @@ class DataListTest extends SapphireTest
$ex = $e;
}
$this->assertInstanceOf(\InvalidArgumentException::class, $ex);
$this->assertRegExp('/Malformed/', $ex->getMessage());
$this->assertMatchesRegularExpression('/Malformed/', $ex->getMessage());
$filter = new ExactMatchFilter('Comments.Max(NonExistentColumn)');
@ -1363,7 +1357,7 @@ class DataListTest extends SapphireTest
$ex = $e;
}
$this->assertInstanceOf(\InvalidArgumentException::class, $ex);
$this->assertRegExp('/Invalid column/', $ex->getMessage());
$this->assertMatchesRegularExpression('/Invalid column/', $ex->getMessage());
}
public function testAggregateFilters()
@ -1699,14 +1693,10 @@ class DataListTest extends SapphireTest
$this->assertListEquals([['Name' => 'Joe']], $list);
}
/**
* Test exact match filter with empty array items
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot filter "DataObjectTest_TeamComment"."Name" against an empty set
*/
public function testEmptyFilter()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot filter "DataObjectTest_TeamComment"."Name" against an empty set');
$list = TeamComment::get();
$list->exclude('Name', []);
}

View File

@ -20,7 +20,7 @@ class DataObjectSchemaGenerationTest extends SapphireTest
SortedObject::class,
];
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
// Start tests
static::start();

View File

@ -21,6 +21,7 @@ use SilverStripe\ORM\Tests\DataObjectTest\Company;
use SilverStripe\ORM\Tests\DataObjectTest\Player;
use SilverStripe\ORM\Tests\DataObjectTest\Team;
use SilverStripe\ORM\Tests\DataObjectTest\TreeNode;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Security\Member;
use SilverStripe\View\ViewableData;
use stdClass;
@ -64,7 +65,7 @@ class DataObjectTest extends SapphireTest
DataObjectTest\TreeNode::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -1483,12 +1484,9 @@ class DataObjectTest extends SapphireTest
$this->assertEquals('New and improved team 1', $reloadedTeam1->Title);
}
/**
* @expectedException \SilverStripe\ORM\ValidationException
*/
public function testWritingInvalidDataObjectThrowsException()
{
$this->expectException(ValidationException::class);
$validatedObject = new DataObjectTest\ValidatedObject();
$validatedObject->write();
}
@ -1638,29 +1636,23 @@ class DataObjectTest extends SapphireTest
$this->assertEquals('Staff', $ceoObj->EmploymentType);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testValidateModelDefinitionsFailsWithArray()
{
$this->expectException(\InvalidArgumentException::class);
Config::modify()->merge(DataObjectTest\Team::class, 'has_one', ['NotValid' => ['NoArraysAllowed']]);
DataObject::getSchema()->hasOneComponent(DataObjectTest\Team::class, 'NotValid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testValidateModelDefinitionsFailsWithIntKey()
{
$this->expectException(\InvalidArgumentException::class);
Config::modify()->set(DataObjectTest\Team::class, 'has_many', [0 => DataObjectTest\Player::class]);
DataObject::getSchema()->hasManyComponent(DataObjectTest\Team::class, 0);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testValidateModelDefinitionsFailsWithIntValue()
{
$this->expectException(\InvalidArgumentException::class);
Config::modify()->merge(DataObjectTest\Team::class, 'many_many', ['Players' => 12]);
DataObject::getSchema()->manyManyComponent(DataObjectTest\Team::class, 'Players');
}
@ -1687,7 +1679,7 @@ class DataObjectTest extends SapphireTest
$this->assertEquals($changedDO->ClassName, DataObjectTest\SubTeam::class);
// Test invalid classes fail
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Controller is not a valid subclass of DataObject');
/**
* @skipUpgrade
@ -2007,7 +1999,7 @@ class DataObjectTest extends SapphireTest
$obj = new DataObjectTest\Fixture();
$obj->write();
$this->assertInternalType("int", $obj->ID);
$this->assertIsInt($obj->ID);
}
/**
@ -2287,11 +2279,9 @@ class DataObjectTest extends SapphireTest
);
}
/**
* @expectedException LogicException
*/
public function testInvalidate()
{
$this->expectException(\LogicException::class);
$do = new DataObjectTest\Fixture();
$do->write();
@ -2382,7 +2372,7 @@ class DataObjectTest extends SapphireTest
$this->assertEquals("PHIL IS A UNIQUE GUY, AND COMMENTS ON TEAM2", $comment->relField('Comment.UpperCase'));
// relField throws exception on invalid properties
$this->expectException(LogicException::class);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("Not is not a relation/field on " . DataObjectTest\TeamComment::class);
$comment->relField('Not.A.Field');
}
@ -2410,7 +2400,7 @@ class DataObjectTest extends SapphireTest
$this->assertEquals("Team 1", $player->relObject('Teams.First.Title')->getValue());
// relObject throws exception on invalid properties
$this->expectException(LogicException::class);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("Not is not a relation/field on " . DataObjectTest\Player::class);
$player->relObject('Not.A.Field');
}
@ -2422,16 +2412,14 @@ class DataObjectTest extends SapphireTest
$this->assertInstanceOf(DataObjectTest\Player::class, DataObjectTest\Player::get()->first());
// You can't pass arguments to LSB syntax - use the DataList methods instead.
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
DataObjectTest\Player::get(null, "\"ID\" = 1");
}
/**
* @expectedException \InvalidArgumentException
*/
public function testBrokenLateStaticBindingStyle()
{
$this->expectException(\InvalidArgumentException::class);
// If you call DataObject::get() you have to pass a first argument
DataObject::get();
}
@ -2456,7 +2444,7 @@ class DataObjectTest extends SapphireTest
public function testSetFieldWithArrayOnScalarOnlyField()
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
$do = Company::singleton();
$do->FoundationYear = '1984';
$do->FoundationYear = ['Amount' => 123, 'Currency' => 'CAD'];
@ -2488,7 +2476,7 @@ class DataObjectTest extends SapphireTest
public function testWriteManipulationWithNonScalarValuesDisallowed()
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
$do = DataObjectTest\MockDynamicAssignmentDataObject::create();
$do->write();

View File

@ -49,7 +49,7 @@ SilverStripe\ORM\Tests\DataObjectTest\Player:
captain1:
FirstName: Captain
Surname: Zookeeper
ShirtNumber: 007
ShirtNumber: '007'
FavouriteTeam: =>SilverStripe\ORM\Tests\DataObjectTest\Team.team1
Teams: =>SilverStripe\ORM\Tests\DataObjectTest\Team.team1
FoundingTeams: =>SilverStripe\ORM\Tests\DataObjectTest\Team.team1

View File

@ -79,12 +79,12 @@ class DataQueryTest extends SapphireTest
$dq = new DataQuery(DataQueryTest\ObjectB::class);
$dq->applyRelation('TestC');
$this->assertTrue($dq->query()->isJoinedTo('testc_DataQueryTest_C'));
$this->assertContains('"testc_DataQueryTest_C"."ID" = "DataQueryTest_B"."TestCID"', $dq->sql());
$this->assertStringContainsString('"testc_DataQueryTest_C"."ID" = "DataQueryTest_B"."TestCID"', $dq->sql());
$dq = new DataQuery(DataQueryTest\ObjectB::class);
$dq->applyRelation('TestCTwo');
$this->assertTrue($dq->query()->isJoinedTo('testctwo_DataQueryTest_C'));
$this->assertContains('"testctwo_DataQueryTest_C"."ID" = "DataQueryTest_B"."TestCTwoID"', $dq->sql());
$this->assertStringContainsString('"testctwo_DataQueryTest_C"."ID" = "DataQueryTest_B"."TestCTwoID"', $dq->sql());
}
public function testApplyRelationDeepInheritance()
@ -94,7 +94,7 @@ class DataQueryTest extends SapphireTest
//apply a relation to a relation from an ancestor class
$newDQ->applyRelation('TestA');
$this->assertTrue($newDQ->query()->isJoinedTo('DataQueryTest_C'));
$this->assertContains('"testa_DataQueryTest_A"."ID" = "DataQueryTest_C"."TestAID"', $newDQ->sql($params));
$this->assertStringContainsString('"testa_DataQueryTest_A"."ID" = "DataQueryTest_C"."TestAID"', $newDQ->sql($params));
//test many_many relation
@ -105,7 +105,7 @@ class DataQueryTest extends SapphireTest
//check we are "joined" to the DataObject's table (there is no distinction between FROM or JOIN clauses)
$this->assertTrue($newDQ->query()->isJoinedTo($baseDBTable));
//check we are explicitly selecting "FROM" the DO's table
$this->assertContains("FROM \"$baseDBTable\"", $newDQ->sql());
$this->assertStringContainsString("FROM \"$baseDBTable\"", $newDQ->sql());
//test many_many with shared inheritance
$newDQ = new DataQuery(DataQueryTest\ObjectE::class);
@ -113,12 +113,12 @@ class DataQueryTest extends SapphireTest
//check we are "joined" to the DataObject's table (there is no distinction between FROM or JOIN clauses)
$this->assertTrue($newDQ->query()->isJoinedTo($baseDBTable));
//check we are explicitly selecting "FROM" the DO's table
$this->assertContains("FROM \"$baseDBTable\"", $newDQ->sql(), 'The FROM clause is missing from the query');
$this->assertStringContainsString("FROM \"$baseDBTable\"", $newDQ->sql(), 'The FROM clause is missing from the query');
$newDQ->applyRelation('ManyTestGs');
//confirm we are still joined to the base table
$this->assertTrue($newDQ->query()->isJoinedTo($baseDBTable));
//double check it is the "FROM" clause
$this->assertContains("FROM \"$baseDBTable\"", $newDQ->sql(), 'The FROM clause has been removed from the query');
$this->assertStringContainsString("FROM \"$baseDBTable\"", $newDQ->sql(), 'The FROM clause has been removed from the query');
//another (potentially less crude check) for checking "FROM" clause
$fromTables = $newDQ->query()->getFrom();
$this->assertEquals('"' . $baseDBTable . '"', $fromTables[$baseDBTable]);
@ -171,6 +171,7 @@ class DataQueryTest extends SapphireTest
$dataQuery = new DataQuery(DataQueryTest\ObjectB::class);
$dataQuery->innerJoin('DataQueryTest_D', '"DataQueryTest_D"."RelationID" = "DataQueryTest_B"."ID"');
$dataQuery->execute();
$this->assertTrue(true);
}
public function testDisjunctiveGroup()
@ -256,7 +257,7 @@ class DataQueryTest extends SapphireTest
{
$dq = new DataQuery(SQLSelectTest\TestObject::class);
$dq = $dq->sort('"Name" ASC, MID("Name", 8, 1) DESC');
$this->assertContains(
$this->assertStringContainsString(
'ORDER BY "SQLSelectTest_DO"."Name" ASC, "_SortColumn0" DESC',
$dq->sql($parameters)
);
@ -272,13 +273,13 @@ class DataQueryTest extends SapphireTest
public function testDistinct()
{
$query = new DataQuery(DataQueryTest\ObjectE::class);
$this->assertContains('SELECT DISTINCT', $query->sql($params), 'Query is set as distinct by default');
$this->assertStringContainsString('SELECT DISTINCT', $query->sql($params), 'Query is set as distinct by default');
$query = $query->distinct(false);
$this->assertNotContains('SELECT DISTINCT', $query->sql($params), 'Query does not contain distinct');
$this->assertStringNotContainsString('SELECT DISTINCT', $query->sql($params), 'Query does not contain distinct');
$query = $query->distinct(true);
$this->assertContains('SELECT DISTINCT', $query->sql($params), 'Query contains distinct');
$this->assertStringContainsString('SELECT DISTINCT', $query->sql($params), 'Query contains distinct');
}
public function testComparisonClauseInt()

View File

@ -206,21 +206,21 @@ class DatabaseTest extends SapphireTest
)->record();
// IDs and ints are returned as ints
$this->assertInternalType('int', $record['ID'], 'Primary key should be integer');
$this->assertInternalType('int', $record['MyInt'], 'DBInt fields should be integer');
$this->assertIsInt($record['ID'], 'Primary key should be integer');
$this->assertIsInt($record['MyInt'], 'DBInt fields should be integer');
$this->assertInternalType('float', $record['MyFloat'], 'DBFloat fields should be float');
$this->assertInternalType('float', $record['MyDecimal'], 'DBDecimal fields should be float');
$this->assertIsFloat($record['MyFloat'], 'DBFloat fields should be float');
$this->assertIsFloat($record['MyDecimal'], 'DBDecimal fields should be float');
// Booleans are returned as ints  we follow MySQL's lead
$this->assertInternalType('int', $record['MyBoolean'], 'DBBoolean fields should be int');
$this->assertIsInt($record['MyBoolean'], 'DBBoolean fields should be int');
// Strings and enums are returned as strings
$this->assertInternalType('string', $record['MyField'], 'DBVarchar fields should be string');
$this->assertInternalType('string', $record['ClassName'], 'DBEnum fields should be string');
$this->assertIsString($record['MyField'], 'DBVarchar fields should be string');
$this->assertIsString($record['ClassName'], 'DBEnum fields should be string');
// Dates are returned as strings
$this->assertInternalType('string', $record['Created'], 'DBDatetime fields should be string');
$this->assertIsString($record['Created'], 'DBDatetime fields should be string');
// Ensure that the same is true when calling a query a second time (cached prepared statement)
@ -231,46 +231,46 @@ class DatabaseTest extends SapphireTest
)->record();
// IDs and ints are returned as ints
$this->assertInternalType('int', $record['ID'], 'Primary key should be integer (2nd call)');
$this->assertInternalType('int', $record['MyInt'], 'DBInt fields should be integer (2nd call)');
$this->assertIsInt($record['ID'], 'Primary key should be integer (2nd call)');
$this->assertIsInt($record['MyInt'], 'DBInt fields should be integer (2nd call)');
$this->assertInternalType('float', $record['MyFloat'], 'DBFloat fields should be float (2nd call)');
$this->assertInternalType('float', $record['MyDecimal'], 'DBDecimal fields should be float (2nd call)');
$this->assertIsFloat($record['MyFloat'], 'DBFloat fields should be float (2nd call)');
$this->assertIsFloat($record['MyDecimal'], 'DBDecimal fields should be float (2nd call)');
// Booleans are returned as ints  we follow MySQL's lead
$this->assertInternalType('int', $record['MyBoolean'], 'DBBoolean fields should be int (2nd call)');
$this->assertIsInt($record['MyBoolean'], 'DBBoolean fields should be int (2nd call)');
// Strings and enums are returned as strings
$this->assertInternalType('string', $record['MyField'], 'DBVarchar fields should be string (2nd call)');
$this->assertInternalType('string', $record['ClassName'], 'DBEnum fields should be string (2nd call)');
$this->assertIsString($record['MyField'], 'DBVarchar fields should be string (2nd call)');
$this->assertIsString($record['ClassName'], 'DBEnum fields should be string (2nd call)');
// Dates are returned as strings
$this->assertInternalType('string', $record['Created'], 'DBDatetime fields should be string (2nd call)');
$this->assertIsString($record['Created'], 'DBDatetime fields should be string (2nd call)');
// Ensure that the same is true when using non-prepared statements
$record = DB::query('SELECT * FROM "DatabaseTest_MyObject" WHERE "ID" = ' . (int)$obj->ID)->record();
// IDs and ints are returned as ints
$this->assertInternalType('int', $record['ID'], 'Primary key should be integer (non-prepared)');
$this->assertInternalType('int', $record['MyInt'], 'DBInt fields should be integer (non-prepared)');
$this->assertIsInt($record['ID'], 'Primary key should be integer (non-prepared)');
$this->assertIsInt($record['MyInt'], 'DBInt fields should be integer (non-prepared)');
$this->assertInternalType('float', $record['MyFloat'], 'DBFloat fields should be float (non-prepared)');
$this->assertInternalType('float', $record['MyDecimal'], 'DBDecimal fields should be float (non-prepared)');
$this->assertIsFloat($record['MyFloat'], 'DBFloat fields should be float (non-prepared)');
$this->assertIsFloat($record['MyDecimal'], 'DBDecimal fields should be float (non-prepared)');
// Booleans are returned as ints  we follow MySQL's lead
$this->assertInternalType('int', $record['MyBoolean'], 'DBBoolean fields should be int (non-prepared)');
$this->assertIsInt($record['MyBoolean'], 'DBBoolean fields should be int (non-prepared)');
// Strings and enums are returned as strings
$this->assertInternalType('string', $record['MyField'], 'DBVarchar fields should be string (non-prepared)');
$this->assertInternalType('string', $record['ClassName'], 'DBEnum fields should be string (non-prepared)');
$this->assertIsString($record['MyField'], 'DBVarchar fields should be string (non-prepared)');
$this->assertIsString($record['ClassName'], 'DBEnum fields should be string (non-prepared)');
// Dates are returned as strings
$this->assertInternalType('string', $record['Created'], 'DBDatetime fields should be string (non-prepared)');
$this->assertIsString($record['Created'], 'DBDatetime fields should be string (non-prepared)');
// Booleans selected directly are ints
$result = DB::query('SELECT TRUE')->first();
$this->assertInternalType('int', reset($result));
$this->assertIsInt(reset($result));
}
/**

View File

@ -19,7 +19,7 @@ class DecimalTest extends SapphireTest
DecimalTest\TestObject::class
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->testDataObject = $this->objFromFixture(DecimalTest\TestObject::class, 'test-dataobject');

View File

@ -28,13 +28,13 @@ class HierachyCacheTest extends SapphireTest
HierarchyOnSubclassTestSubObject::class
];
public function setUp()
protected function setUp(): void
{
parent::setUp();
TestObject::singleton()->flushCache();
}
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
HideTestObject::config()->update(

View File

@ -27,7 +27,7 @@ class HierarchyTest extends SapphireTest
return [];
}
public function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -2,7 +2,8 @@
namespace SilverStripe\ORM\Tests;
use PHPUnit_Framework_MockObject_MockObject;
use LogicException;
use PHPUnit\Framework\MockObject\MockObject;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\ListDecorator;
@ -14,16 +15,16 @@ use SilverStripe\ORM\SS_List;
class ListDecoratorTest extends SapphireTest
{
/**
* @var ArrayList|PHPUnit_Framework_MockObject_MockObject
* @var ArrayList|MockObject
*/
protected $list;
/**
* @var ListDecorator|PHPUnit_Framework_MockObject_MockObject
* @var ListDecorator|MockObject
*/
protected $decorator;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -108,12 +109,10 @@ class ListDecoratorTest extends SapphireTest
$this->assertFalse($this->decorator->canFilterBy('Title'));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage SS_Filterable::filterByCallback() passed callback must be callable, 'boolean' given
*/
public function testFilterByCallbackThrowsExceptionWhenGivenNonCallable()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("SS_Filterable::filterByCallback() passed callback must be callable, 'boolean' given");
$this->decorator->filterByCallback(true);
}

View File

@ -444,7 +444,7 @@ class ManyManyListTest extends SapphireTest
public function testWriteManipulationWithNonScalarValuesDisallowed()
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
$left = DataObjectTest\MockDynamicAssignmentDataObject::create();
$left->write();

View File

@ -2,6 +2,7 @@
namespace SilverStripe\ORM\Tests;
use InvalidArgumentException;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\DataObject;
@ -31,13 +32,13 @@ class ManyManyThroughListTest extends SapphireTest
ManyManyThroughListTest\FallbackLocale::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
DataObject::reset();
}
protected function tearDown()
protected function tearDown(): void
{
DataObject::reset();
parent::tearDown();
@ -204,11 +205,10 @@ class ManyManyThroughListTest extends SapphireTest
/**
* Test validation
*
* @expectedException \InvalidArgumentException
*/
public function testValidateModelValidatesJoinType()
{
$this->expectException(\InvalidArgumentException::class);
DataObject::reset();
ManyManyThroughListTest\Item::config()->update(
'db',

View File

@ -31,7 +31,7 @@ class MarkedSetTest extends SapphireTest
return [];
}
public function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -329,7 +329,7 @@ class PaginatedListTest extends SapphireTest
public function testFirstLink()
{
$list = new PaginatedList(new ArrayList());
$this->assertContains('start=0', $list->FirstLink());
$this->assertStringContainsString('start=0', $list->FirstLink());
}
public function testFirstLinkContainsCurrentGetParameters()
@ -358,11 +358,11 @@ class PaginatedListTest extends SapphireTest
$list = new PaginatedList(new ArrayList());
$list->setPageLength(10);
$list->setTotalItems(100);
$this->assertContains('start=90', $list->LastLink());
$this->assertStringContainsString('start=90', $list->LastLink());
// Disable paging
$list->setPageLength(0);
$this->assertContains('start=0', $list->LastLink());
$this->assertStringContainsString('start=0', $list->LastLink());
}
public function testLastLinkContainsCurrentGetParameters()
@ -391,13 +391,13 @@ class PaginatedListTest extends SapphireTest
$list = new PaginatedList(new ArrayList());
$list->setTotalItems(50);
$this->assertContains('start=10', $list->NextLink());
$this->assertStringContainsString('start=10', $list->NextLink());
$list->setCurrentPage(2);
$this->assertContains('start=20', $list->NextLink());
$this->assertStringContainsString('start=20', $list->NextLink());
$list->setCurrentPage(3);
$this->assertContains('start=30', $list->NextLink());
$this->assertStringContainsString('start=30', $list->NextLink());
$list->setCurrentPage(4);
$this->assertContains('start=40', $list->NextLink());
$this->assertStringContainsString('start=40', $list->NextLink());
$list->setCurrentPage(5);
$this->assertNull($list->NextLink());
@ -435,11 +435,11 @@ class PaginatedListTest extends SapphireTest
$this->assertNull($list->PrevLink());
$list->setCurrentPage(2);
$this->assertContains('start=0', $list->PrevLink());
$this->assertStringContainsString('start=0', $list->PrevLink());
$list->setCurrentPage(3);
$this->assertContains('start=10', $list->PrevLink());
$this->assertStringContainsString('start=10', $list->PrevLink());
$list->setCurrentPage(5);
$this->assertContains('start=30', $list->PrevLink());
$this->assertStringContainsString('start=30', $list->PrevLink());
// Disable paging
$list->setPageLength(0);

View File

@ -2,6 +2,7 @@
namespace SilverStripe\ORM\Tests;
use InvalidArgumentException;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\Connect\MySQLDatabase;
use SilverStripe\ORM\Queries\SQLSelect;
@ -23,13 +24,13 @@ class SQLSelectTest extends SapphireTest
protected $oldDeprecation = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->oldDeprecation = Deprecation::dump_settings();
}
protected function tearDown()
protected function tearDown(): void
{
Deprecation::restore_settings($this->oldDeprecation);
parent::tearDown();
@ -43,7 +44,7 @@ class SQLSelectTest extends SapphireTest
$ids = $this->allFixtureIDs(SQLSelectTest\TestObject::class);
$count = $qry->count('"SQLSelectTest_DO"."ID"');
$this->assertEquals(count($ids), $count);
$this->assertInternalType("int", $count);
$this->assertIsInt($count);
//test with `having`
if (DB::get_conn() instanceof MySQLDatabase) {
$qry->setSelect([
@ -54,7 +55,7 @@ class SQLSelectTest extends SapphireTest
$qry->setHaving('"Date" > 2012-02-01');
$count = $qry->count('"SQLSelectTest_DO"."ID"');
$this->assertEquals(1, $count);
$this->assertInternalType("int", $count);
$this->assertIsInt($count);
}
}
public function testUnlimitedRowCount()
@ -65,17 +66,17 @@ class SQLSelectTest extends SapphireTest
$qry->setLimit(1);
$count = $qry->unlimitedRowCount('"SQLSelectTest_DO"."ID"');
$this->assertEquals(count($ids), $count);
$this->assertInternalType("int", $count);
$this->assertIsInt($count);
// Test without column - SQLSelect has different logic for this
$count = $qry->unlimitedRowCount();
$this->assertEquals(2, $count);
$this->assertInternalType("int", $count);
$this->assertIsInt($count);
//test with `having`
if (DB::get_conn() instanceof MySQLDatabase) {
$qry->setHaving('"Date" > 2012-02-01');
$count = $qry->unlimitedRowCount('"SQLSelectTest_DO"."ID"');
$this->assertEquals(1, $count);
$this->assertInternalType("int", $count);
$this->assertIsInt($count);
}
}
@ -267,57 +268,23 @@ class SQLSelectTest extends SapphireTest
);
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testZeroLimit()
{
Deprecation::notification_version('4.3.0');
$query = new SQLSelect();
$query->setFrom("MyTable");
$query->setLimit(0);
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testZeroLimitWithOffset()
{
Deprecation::notification_version('4.3.0');
if (!(DB::get_conn() instanceof MySQLDatabase || DB::get_conn() instanceof SQLite3Database
|| DB::get_conn() instanceof PostgreSQLDatabase)
) {
$this->markTestIncomplete();
}
$query = new SQLSelect();
$query->setFrom("MyTable");
$query->setLimit(0, 99);
}
/**
* @expectedException InvalidArgumentException
*/
public function testNegativeLimit()
{
$this->expectException(\InvalidArgumentException::class);
$query = new SQLSelect();
$query->setLimit(-10);
}
/**
* @expectedException InvalidArgumentException
*/
public function testNegativeOffset()
{
$this->expectException(\InvalidArgumentException::class);
$query = new SQLSelect();
$query->setLimit(1, -10);
}
/**
* @expectedException InvalidArgumentException
*/
public function testNegativeOffsetAndLimit()
{
$this->expectException(\InvalidArgumentException::class);
$query = new SQLSelect();
$query->setLimit(-10, -10);
}

View File

@ -12,7 +12,7 @@ use SilverStripe\ORM\Search\FulltextSearchable;
class FulltextSearchableTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -24,7 +24,7 @@ class FulltextSearchableTest extends SapphireTest
* properly at the end of the test. This becomes apparent when a later test tries to
* ALTER TABLE File and add fulltext indexes with the InnoDB table type.
*/
protected function tearDown()
protected function tearDown(): void
{
parent::tearDown();

View File

@ -20,19 +20,19 @@ class TransactionTest extends SapphireTest
private static $originalVersionInfo;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
self::$originalVersionInfo = Deprecation::dump_settings();
}
protected function tearDown()
protected function tearDown(): void
{
Deprecation::restore_settings(self::$originalVersionInfo);
parent::tearDown();
}
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
if (!DB::get_conn()->supportsTransactions()) {
@ -192,7 +192,7 @@ class TransactionTest extends SapphireTest
$fail = DataObject::get_one(TestObject::class, "\"Title\"='Read only page failed'");
//This page should be in the system
$this->assertInternalType('object', $success);
$this->assertIsObject($success);
$this->assertTrue($success->exists());
//This page should NOT exist, we had 'read only' permissions

View File

@ -8,6 +8,16 @@ use SilverStripe\Dev\SapphireTest;
class ValidationExceptionTest extends SapphireTest
{
private function arrayContainsArray($expectedSubArray, $array)
{
foreach ($array as $subArray) {
if ($subArray == $expectedSubArray) {
return true;
}
}
return false;
}
/**
* Test that ValidationResult object can correctly populate a ValidationException
*/
@ -21,15 +31,13 @@ class ValidationExceptionTest extends SapphireTest
$this->assertEquals(0, $exception->getCode());
$this->assertEquals('Not a valid result', $exception->getMessage());
$this->assertFalse($exception->getResult()->isValid());
$this->assertContains(
[
$b = $this->arrayContainsArray([
'message' => 'Not a valid result',
'messageCast' => ValidationResult::CAST_TEXT,
'messageType' => ValidationResult::TYPE_ERROR,
'fieldName' => null,
],
$exception->getResult()->getMessages()
);
], $exception->getResult()->getMessages());
$this->assertTrue($b, 'Messages array should contain expected messaged');
}
/**
@ -47,24 +55,22 @@ class ValidationExceptionTest extends SapphireTest
$this->assertEquals(0, $exception->getCode());
$this->assertEquals('Invalid type', $exception->getMessage());
$this->assertEquals(false, $exception->getResult()->isValid());
$this->assertContains(
[
$b = $this->arrayContainsArray([
'message' => 'Invalid type',
'messageCast' => ValidationResult::CAST_TEXT,
'messageType' => ValidationResult::TYPE_ERROR,
'fieldName' => null,
],
$exception->getResult()->getMessages()
);
$this->assertContains(
[
], $exception->getResult()->getMessages());
$this->assertTrue($b, 'Messages array should contain expected messaged');
$b = $this->arrayContainsArray([
'message' => 'Out of kiwis',
'messageCast' => ValidationResult::CAST_TEXT,
'messageType' => ValidationResult::TYPE_ERROR,
'fieldName' => null,
],
$exception->getResult()->getMessages()
);
], $exception->getResult()->getMessages());
$this->assertTrue($b, 'Messages array should contain expected messaged');
}
/**
@ -78,15 +84,14 @@ class ValidationExceptionTest extends SapphireTest
$this->assertEquals(E_USER_ERROR, $exception->getCode());
$this->assertEquals('Error inferred from message', $exception->getMessage());
$this->assertFalse($exception->getResult()->isValid());
$this->assertContains(
[
$b = $this->arrayContainsArray([
'message' => 'Error inferred from message',
'messageCast' => ValidationResult::CAST_TEXT,
'messageType' => ValidationResult::TYPE_ERROR,
'fieldName' => null,
],
$exception->getResult()->getMessages()
);
], $exception->getResult()->getMessages());
$this->assertTrue($b, 'Messages array should contain expected messaged');
}
/**
@ -103,24 +108,22 @@ class ValidationExceptionTest extends SapphireTest
$this->assertEquals(E_USER_WARNING, $exception->getCode());
$this->assertEquals('A spork is not a knife', $exception->getMessage());
$this->assertEquals(false, $exception->getResult()->isValid());
$this->assertContains(
[
$b = $this->arrayContainsArray([
'message' => 'A spork is not a knife',
'messageCast' => ValidationResult::CAST_TEXT,
'messageType' => ValidationResult::TYPE_ERROR,
'fieldName' => null,
],
$exception->getResult()->getMessages()
);
$this->assertContains(
[
], $exception->getResult()->getMessages());
$this->assertTrue($b, 'Messages array should contain expected messaged');
$b = $this->arrayContainsArray([
'message' => 'A knife is not a back scratcher',
'messageCast' => ValidationResult::CAST_TEXT,
'messageType' => ValidationResult::TYPE_ERROR,
'fieldName' => null,
],
$exception->getResult()->getMessages()
);
], $exception->getResult()->getMessages());
$this->assertTrue($b, 'Messages array should contain expected messaged');
}
/**
@ -175,30 +178,30 @@ class ValidationExceptionTest extends SapphireTest
$this->assertEquals(
[
[
'fieldName' => null,
'message' => 'A spork is not a knife',
'messageType' => 'bad',
'messageCast' => ValidationResult::CAST_TEXT,
],
[
'fieldName' => null,
'message' => 'A knife is not a back scratcher',
'messageType' => 'error',
'messageCast' => ValidationResult::CAST_TEXT,
],
[
'fieldName' => 'Title',
'message' => 'Title is good',
'messageType' => 'good',
'messageCast' => ValidationResult::CAST_TEXT,
],
[
'fieldName' => 'Content',
'message' => 'Content is bad',
'messageType' => 'bad',
'messageCast' => ValidationResult::CAST_TEXT,
]
[
'fieldName' => null,
'message' => 'A spork is not a knife',
'messageType' => 'bad',
'messageCast' => ValidationResult::CAST_TEXT,
],
[
'fieldName' => null,
'message' => 'A knife is not a back scratcher',
'messageType' => 'error',
'messageCast' => ValidationResult::CAST_TEXT,
],
[
'fieldName' => 'Title',
'message' => 'Title is good',
'messageType' => 'good',
'messageCast' => ValidationResult::CAST_TEXT,
],
[
'fieldName' => 'Content',
'message' => 'Content is bad',
'messageType' => 'bad',
'messageCast' => ValidationResult::CAST_TEXT,
]
],
$result->getMessages()
);

View File

@ -26,7 +26,7 @@ class BasicAuthTest extends FunctionalTest
ControllerNotSecured::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -20,7 +20,7 @@ class GroupTest extends FunctionalTest
TestMember::class
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
}
@ -164,7 +164,7 @@ class GroupTest extends FunctionalTest
*/
public function testCannotCollateUnsavedGroupFamilyIds()
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot call collateFamilyIDs on unsaved Group.');
$group = new Group;
$group->collateFamilyIDs();

View File

@ -15,7 +15,7 @@ class InheritedPermissionsFlusherTest extends SapphireTest
{
protected static $fixture_file = 'InheritedPermissionsFlusherTest.yml';
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -29,7 +29,7 @@ class InheritedPermissionsTest extends SapphireTest
*/
protected $rootPermissions = null;
protected function setUp()
protected function setUp(): void
{
$this->rootPermissions = new TestDefaultPermissionChecker();
@ -57,7 +57,7 @@ class InheritedPermissionsTest extends SapphireTest
$permission2->clearCache();
}
protected function tearDown()
protected function tearDown(): void
{
Injector::inst()->unregisterNamedObject(PermissionChecker::class . '.testpermissions');
Injector::inst()->unregisterNamedObject(PermissionChecker::class . '.unstagedpermissions');

View File

@ -15,7 +15,7 @@ class ChangePasswordHandlerTest extends SapphireTest
{
protected static $fixture_file = 'ChangePasswordHandlerTest.yml';
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -42,8 +42,8 @@ class ChangePasswordHandlerTest extends SapphireTest
$result = $handler->setRequest($request)->changepassword();
$this->assertInternalType('array', $result, 'An array is returned');
$this->assertContains('Security/lostpassword', $result['Content'], 'Lost password URL is included');
$this->assertContains('Security/login', $result['Content'], 'Login URL is included');
$this->assertIsArray($result, 'An array is returned');
$this->assertStringContainsString('Security/lostpassword', $result['Content'], 'Lost password URL is included');
$this->assertStringContainsString('Security/login', $result['Content'], 'Login URL is included');
}
}

View File

@ -32,7 +32,7 @@ class MemberAuthenticatorTest extends SapphireTest
protected $defaultUsername = null;
protected $defaultPassword = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -52,7 +52,7 @@ class MemberAuthenticatorTest extends SapphireTest
->setTestNames([]);
}
protected function tearDown()
protected function tearDown(): void
{
DefaultAdminService::clearDefaultAdmin();
if ($this->defaultUsername) {

View File

@ -14,7 +14,7 @@ class MemberCsvBulkLoaderTest extends SapphireTest
{
protected static $fixture_file = 'MemberCsvBulkLoaderTest.yml';
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -39,7 +39,7 @@ class MemberTest extends FunctionalTest
Member::class => '*',
];
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
@ -63,7 +63,7 @@ class MemberTest extends FunctionalTest
/**
* @skipUpgrade
*/
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -104,11 +104,10 @@ class MemberTest extends FunctionalTest
$m2->write();
}
/**
* @expectedException \SilverStripe\ORM\ValidationException
*/
public function testWriteDoesntMergeExistingMemberOnIdentifierChange()
{
$this->expectException(ValidationException::class);
$m1 = new Member();
$m1->Email = 'member@test.com';
$m1->write();
@ -256,7 +255,7 @@ class MemberTest extends FunctionalTest
$this->assertEquals($response->getStatusCode(), 302);
// We should get redirected to Security/passwordsent
$this->assertContains(
$this->assertStringContainsString(
'Security/lostpassword/passwordsent',
urldecode($response->getHeader('Location'))
);
@ -1011,7 +1010,7 @@ class MemberTest extends FunctionalTest
['name' => $m1->FirstName]
)
);
$this->assertContains($message, $response->getBody());
$this->assertStringContainsString($message, $response->getBody());
$this->logOut();
@ -1025,7 +1024,7 @@ class MemberTest extends FunctionalTest
'alc_device' => $firstHash->DeviceID
]
);
$this->assertNotContains($message, $response->getBody());
$this->assertStringNotContainsString($message, $response->getBody());
$response = $this->get(
'Security/login',
@ -1036,7 +1035,7 @@ class MemberTest extends FunctionalTest
'alc_device' => str_rot13($firstHash->DeviceID)
]
);
$this->assertNotContains($message, $response->getBody());
$this->assertStringNotContainsString($message, $response->getBody());
// Re-logging (ie 'alc_enc' has expired), and not checking the "Remember Me" option
// should remove all previous hashes for this device
@ -1054,7 +1053,7 @@ class MemberTest extends FunctionalTest
'alc_device' => $firstHash->DeviceID
]
);
$this->assertContains($message, $response->getBody());
$this->assertStringContainsString($message, $response->getBody());
$this->assertEquals(RememberLoginHash::get()->filter('MemberID', $m1->ID)->count(), 0);
}
@ -1091,7 +1090,7 @@ class MemberTest extends FunctionalTest
['name' => $m1->FirstName]
)
);
$this->assertContains($message, $response->getBody());
$this->assertStringContainsString($message, $response->getBody());
$this->logOut();
@ -1112,7 +1111,7 @@ class MemberTest extends FunctionalTest
'alc_device' => $firstHash->DeviceID
]
);
$this->assertNotContains($message, $response->getBody());
$this->assertStringNotContainsString($message, $response->getBody());
$this->logOut();
DBDatetime::clear_mock_now();
}
@ -1167,7 +1166,7 @@ class MemberTest extends FunctionalTest
['name' => $m1->FirstName]
)
);
$this->assertContains($message, $response->getBody());
$this->assertStringContainsString($message, $response->getBody());
// Test that removing session but not cookie keeps user
/** @var SessionAuthenticationHandler $sessionHandler */
@ -1185,7 +1184,7 @@ class MemberTest extends FunctionalTest
'alc_device' => $secondHash->DeviceID
]
);
$this->assertContains($message, $response->getBody());
$this->assertStringContainsString($message, $response->getBody());
// Logging out from the second device - only one device being logged out
RememberLoginHash::config()->update('logout_across_devices', false);
@ -1608,6 +1607,7 @@ class MemberTest extends FunctionalTest
public function testChangePasswordToBlankIsValidated()
{
Member::set_password_validator(new PasswordValidator());
// override setup() function which setMinLength(0)
PasswordValidator::singleton()->setMinLength(8);
// 'test' member has a password defined in yml

View File

@ -7,12 +7,13 @@ use SilverStripe\Security\PasswordEncryptor;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\PasswordEncryptor_LegacyPHPHash;
use SilverStripe\Security\PasswordEncryptor_NotFoundException;
use SilverStripe\Security\PasswordEncryptor_PHPHash;
use SilverStripe\Security\Tests\PasswordEncryptorTest\TestEncryptor;
class PasswordEncryptorTest extends SapphireTest
{
protected function tearDown()
protected function tearDown(): void
{
parent::tearDown();
PasswordEncryptor_Blowfish::set_cost(10);
@ -29,11 +30,9 @@ class PasswordEncryptorTest extends SapphireTest
$this->assertInstanceOf(TestEncryptor::class, $e);
}
/**
* @expectedException \SilverStripe\Security\PasswordEncryptor_NotFoundException
*/
public function testCreateForCodeNotFound()
{
$this->expectException(PasswordEncryptor_NotFoundException::class);
PasswordEncryptor::create_for_algorithm('unknown');
}
@ -47,7 +46,7 @@ class PasswordEncryptorTest extends SapphireTest
$encryptors = PasswordEncryptor::get_encryptors();
$this->assertContains('test', array_keys($encryptors));
$encryptor = $encryptors['test'];
$this->assertContains(TestEncryptor::class, key($encryptor));
$this->assertStringContainsString(TestEncryptor::class, key($encryptor));
}
public function testEncryptorPHPHashWithArguments()

View File

@ -14,7 +14,7 @@ class PasswordExpirationMiddlewareTest extends SapphireTest
{
use HttpRequestMockBuilder;
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -14,7 +14,7 @@ class PasswordValidatorTest extends SapphireTest
*/
protected $usesDatabase = true;
protected function setUp()
protected function setUp(): void
{
parent::setUp();

View File

@ -30,8 +30,8 @@ class PermissionCheckboxSetFieldTest extends SapphireTest
$f->getHiddenPermissions(),
['NON-ADMIN']
);
$this->assertContains('ADMIN', $f->Field());
$this->assertNotContains('NON-ADMIN', $f->Field());
$this->assertStringContainsString('ADMIN', $f->Field());
$this->assertStringNotContainsString('NON-ADMIN', $f->Field());
}
public function testSaveInto()

View File

@ -143,14 +143,14 @@ class PermissionTest extends SapphireTest
public function testHiddenPermissions()
{
$permissionCheckboxSet = new PermissionCheckboxSetField('Permissions', 'Permissions', Permission::class, 'GroupID');
$this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
$this->assertStringContainsString('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
Config::modify()->merge(Permission::class, 'hidden_permissions', ['CMS_ACCESS_LeftAndMain']);
$this->assertNotContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
$this->assertStringNotContainsString('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
Config::inst()->remove(Permission::class, 'hidden_permissions');
$this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
$this->assertStringContainsString('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
}
public function testEmptyMemberFails()

View File

@ -14,7 +14,7 @@ class RememberLoginHashTest extends SapphireTest
/** @var RememberLoginHash[] */
private $loginHash = [];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -43,12 +43,12 @@ class RememberLoginHashTest extends SapphireTest
/**
* @param bool $logoutAcrossDevices
* @param string $deviceId
* @param mixed $deviceId
* @param array $expected
* @param array $unexpected
* @dataProvider clearScenarios
*/
public function testClear(bool $logoutAcrossDevices, string $deviceId, array $expected, array $unexpected)
public function testClear(bool $logoutAcrossDevices, $deviceId, array $expected, array $unexpected)
{
// If session-manager module is installed then logout_across_devices is modified so skip
if (class_exists(LoginSession::class)) {

View File

@ -16,7 +16,7 @@ class SecurityDefaultAdminTest extends SapphireTest
protected $defaultPassword = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
@ -39,7 +39,7 @@ class SecurityDefaultAdminTest extends SapphireTest
Permission::reset();
}
protected function tearDown()
protected function tearDown(): void
{
DefaultAdminService::clearDefaultAdmin();
if ($this->defaultUsername) {

View File

@ -42,7 +42,7 @@ class SecurityTest extends FunctionalTest
SecurityTest\SecuredController::class,
];
protected function setUp()
protected function setUp(): void
{
// Set to an empty array of authenticators to enable the default
Config::modify()->set(MemberAuthenticator::class, 'authenticators', []);
@ -82,7 +82,7 @@ class SecurityTest extends FunctionalTest
$response = $this->get('SecurityTest_SecuredController');
$this->assertEquals(302, $response->getStatusCode());
$this->assertContains(
$this->assertStringContainsString(
Config::inst()->get(Security::class, 'login_url'),
$response->getHeader('Location')
);
@ -90,7 +90,7 @@ class SecurityTest extends FunctionalTest
$this->logInWithPermission('ADMIN');
$response = $this->get('SecurityTest_SecuredController');
$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('Success', $response->getBody());
$this->assertStringContainsString('Success', $response->getBody());
$this->autoFollowRedirection = true;
}
@ -135,7 +135,7 @@ class SecurityTest extends FunctionalTest
['default' => 'default', 'alreadyLoggedIn' => 'You are already logged in!']
);
Security::permissionFailure($controller);
$this->assertContains(
$this->assertStringContainsString(
'You are already logged in!',
$controller->getResponse()->getBody(),
'Custom permission failure message was ignored'
@ -145,7 +145,7 @@ class SecurityTest extends FunctionalTest
$controller,
['default' => 'default', 'alreadyLoggedIn' => 'One-off failure message']
);
$this->assertContains(
$this->assertStringContainsString(
'One-off failure message',
$controller->getResponse()->getBody(),
"Message set passed to Security::permissionFailure() didn't override Config values"
@ -156,7 +156,7 @@ class SecurityTest extends FunctionalTest
$controller,
DBField::create_field('HTMLFragment', '<p>Custom HTML &amp; Message</p>')
);
$this->assertContains(
$this->assertStringContainsString(
'<p>Custom HTML &amp; Message</p>',
$controller->getResponse()->getBody()
);
@ -166,7 +166,7 @@ class SecurityTest extends FunctionalTest
$controller,
DBField::create_field('Text', 'Safely escaped & message')
);
$this->assertContains(
$this->assertStringContainsString(
'Safely escaped &amp; message',
$controller->getResponse()->getBody()
);
@ -196,29 +196,29 @@ class SecurityTest extends FunctionalTest
Security::setCurrentUser(null);
}
$response = $this->getRecursive('SecurityTest_SecuredController');
$this->assertContains(Convert::raw2xml("That page is secured."), $response->getBody());
$this->assertContains('<input type="submit" name="action_doLogin"', $response->getBody());
$this->assertStringContainsString(Convert::raw2xml("That page is secured."), $response->getBody());
$this->assertStringContainsString('<input type="submit" name="action_doLogin"', $response->getBody());
// Non-logged in user should not be redirected, but instead shown the login form
// No message/context is available as the user has not attempted to view the secured controller
$response = $this->getRecursive('Security/login?BackURL=SecurityTest_SecuredController/');
$this->assertNotContains(Convert::raw2xml("That page is secured."), $response->getBody());
$this->assertNotContains(Convert::raw2xml("You don't have access to this page"), $response->getBody());
$this->assertContains('<input type="submit" name="action_doLogin"', $response->getBody());
$this->assertStringNotContainsString(Convert::raw2xml("That page is secured."), $response->getBody());
$this->assertStringNotContainsString(Convert::raw2xml("You don't have access to this page"), $response->getBody());
$this->assertStringContainsString('<input type="submit" name="action_doLogin"', $response->getBody());
// BackURL with permission error (wrong permissions) should not redirect
$this->logInAs('grouplessmember');
$response = $this->getRecursive('SecurityTest_SecuredController');
$this->assertContains(Convert::raw2xml("You don't have access to this page"), $response->getBody());
$this->assertContains(
$this->assertStringContainsString(Convert::raw2xml("You don't have access to this page"), $response->getBody());
$this->assertStringContainsString(
'<input type="submit" name="action_logout" value="Log in as someone else"',
$response->getBody()
);
// Directly accessing this page should attempt to follow the BackURL, but stop when it encounters the error
$response = $this->getRecursive('Security/login?BackURL=SecurityTest_SecuredController/');
$this->assertContains(Convert::raw2xml("You don't have access to this page"), $response->getBody());
$this->assertContains(
$this->assertStringContainsString(Convert::raw2xml("You don't have access to this page"), $response->getBody());
$this->assertStringContainsString(
'<input type="submit" name="action_logout" value="Log in as someone else"',
$response->getBody()
);
@ -226,11 +226,11 @@ class SecurityTest extends FunctionalTest
// Check correctly logged in admin doesn't generate the same errors
$this->logInAs('admin');
$response = $this->getRecursive('SecurityTest_SecuredController');
$this->assertContains(Convert::raw2xml("Success"), $response->getBody());
$this->assertStringContainsString(Convert::raw2xml("Success"), $response->getBody());
// Directly accessing this page should attempt to follow the BackURL and succeed
$response = $this->getRecursive('Security/login?BackURL=SecurityTest_SecuredController/');
$this->assertContains(Convert::raw2xml("Success"), $response->getBody());
$this->assertStringContainsString(Convert::raw2xml("Success"), $response->getBody());
}
public function testLogInAsSomeoneElse()
@ -356,7 +356,7 @@ class SecurityTest extends FunctionalTest
/* We get a good response */
$this->assertEquals(302, $response->getStatusCode());
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/testpage/',
$response->getHeader('Location'),
"Logout form redirects to back to referer."
@ -376,7 +376,7 @@ class SecurityTest extends FunctionalTest
// Test internal relative redirect
$response = $this->doTestLoginForm('noexpiry@silverstripe.com', '1nitialPassword', 'testpage');
$this->assertEquals(302, $response->getStatusCode());
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/testpage/',
$response->getHeader('Location'),
"Internal relative BackURLs work when passed through to login form"
@ -391,7 +391,7 @@ class SecurityTest extends FunctionalTest
Director::absoluteBaseURL() . 'testpage'
);
// for some reason the redirect happens to a relative URL
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/^' . preg_quote(Director::absoluteBaseURL(), '/') . 'testpage/',
$response->getHeader('Location'),
"Internal absolute BackURLs work when passed through to login form"
@ -401,7 +401,7 @@ class SecurityTest extends FunctionalTest
// Test external redirect
$response = $this->doTestLoginForm('noexpiry@silverstripe.com', '1nitialPassword', 'http://myspoofedhost.com');
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/^' . preg_quote('http://myspoofedhost.com', '/') . '/',
(string)$response->getHeader('Location'),
"Redirection to external links in login form BackURL gets prevented as a measure against spoofing attacks"
@ -410,7 +410,7 @@ class SecurityTest extends FunctionalTest
// Test external redirection on ChangePasswordForm
$this->get('Security/changepassword?BackURL=http://myspoofedhost.com');
$changedResponse = $this->doTestChangepasswordForm('1nitialPassword', 'changedPassword#123');
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/^' . preg_quote('http://myspoofedhost.com', '/') . '/',
(string)$changedResponse->getHeader('Location'),
"Redirection to external links in change password form BackURL gets prevented to stop spoofing attacks"
@ -428,7 +428,7 @@ class SecurityTest extends FunctionalTest
/* BAD PASSWORDS ARE LOCKED OUT */
$badResponse = $this->doTestLoginForm('testuser@example.com', 'badpassword');
$this->assertEquals(302, $badResponse->getStatusCode());
$this->assertRegExp('/Security\/login/', $badResponse->getHeader('Location'));
$this->assertMatchesRegularExpression('/Security\/login/', $badResponse->getHeader('Location'));
$this->assertNull($this->session()->get('loggedInAs'));
/* UNEXPIRED PASSWORD GO THROUGH WITHOUT A HITCH */
@ -714,7 +714,7 @@ class SecurityTest extends FunctionalTest
$response = $this->get(Config::inst()->get(Security::class, 'login_url'));
$robotsHeader = $response->getHeader('X-Robots-Tag');
$this->assertNotNull($robotsHeader);
$this->assertContains('noindex', $robotsHeader);
$this->assertStringContainsString('noindex', $robotsHeader);
}
public function testDoNotSendEmptyRobotsHeaderIfNotDefined()
@ -812,7 +812,7 @@ class SecurityTest extends FunctionalTest
}
}
$this->assertContains($expected, $messages, $errorMessage);
$this->assertContains($expected, $messages, $errorMessage ?: '');
}
/**

View File

@ -36,7 +36,7 @@ class VersionedMemberAuthenticatorTest extends SapphireTest
]
];
public function setUp()
protected function setUp(): void
{
parent::setUp();
@ -51,7 +51,7 @@ class VersionedMemberAuthenticatorTest extends SapphireTest
->setTestNames([]);
}
protected function tearDown()
protected function tearDown(): void
{
$this->logOut();
parent::tearDown();

View File

@ -53,14 +53,6 @@ class ArrayDataTest extends SapphireTest
{
$array = [0 => "One", 1 => "Two"];
$this->assertFalse(ArrayLib::is_associative($array));
/*
* Expect user_error() to be called below, if enabled
* (tobych) That should be an exception. Something like:
* $this->setExpectedException('InvalidArgumentException');
*/
// $arrayData = new ArrayData($array);
}
public function testSetField()

View File

@ -60,21 +60,21 @@ class ContentNegotiatorTest extends SapphireTest
////////////////////////
// XHTML select options
////////////////////////
$this->assertRegExp('/<option>aa<\/option>/', $response->getBody());
$this->assertRegExp('/<option selected = "selected">bb<\/option>/', $response->getBody());
$this->assertRegExp('/<option selected="selected">cc<\/option>/', $response->getBody());
$this->assertMatchesRegularExpression('/<option>aa<\/option>/', $response->getBody());
$this->assertMatchesRegularExpression('/<option selected = "selected">bb<\/option>/', $response->getBody());
$this->assertMatchesRegularExpression('/<option selected="selected">cc<\/option>/', $response->getBody());
// Just transform this
$this->assertRegExp('/<option class="foo" selected="selected">dd<\/option>/', $response->getBody());
$this->assertRegExp('/<option selected="selected" value="">ll<\/option>/', $response->getBody());
$this->assertMatchesRegularExpression('/<option class="foo" selected="selected">dd<\/option>/', $response->getBody());
$this->assertMatchesRegularExpression('/<option selected="selected" value="">ll<\/option>/', $response->getBody());
////////////////////////////////////////////////
// XHTML checkbox options + XHTML input closure
////////////////////////////////////////////////
$this->assertRegExp('/<input type="checkbox"\/>ff/', $response->getBody());
$this->assertRegExp('/<input type="checkbox" checked = "checked"\/>g/', $response->getBody());
$this->assertRegExp('/<input type="checkbox" checked="checked"\/>hh/', $response->getBody());
$this->assertMatchesRegularExpression('/<input type="checkbox"\/>ff/', $response->getBody());
$this->assertMatchesRegularExpression('/<input type="checkbox" checked = "checked"\/>g/', $response->getBody());
$this->assertMatchesRegularExpression('/<input type="checkbox" checked="checked"\/>hh/', $response->getBody());
// Just transform this
$this->assertRegExp('/<input class="bar" type="checkbox" checked="checked"\/>ii/', $response->getBody());
$this->assertRegExp('/<input type="checkbox" checked="checked" class="foo"\/>jj/', $response->getBody());
$this->assertMatchesRegularExpression('/<input class="bar" type="checkbox" checked="checked"\/>ii/', $response->getBody());
$this->assertMatchesRegularExpression('/<input type="checkbox" checked="checked" class="foo"\/>jj/', $response->getBody());
}
}

View File

@ -57,7 +57,7 @@ class HTMLTest extends SapphireTest
public function testVoidContentError()
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Void element \"link\" cannot have content");
HTML::createTag('link', [

View File

@ -75,7 +75,7 @@ class DiffTest extends SapphireTest
$expected = "/^ *<del>$sentenceOne<\/del> *$sentenceTwo *<ins>$sentenceOne<\/ins> *$/";
$actual = Diff::compareHTML($from, $to);
$this->assertRegExp($expected, $actual);
$this->assertMatchesRegularExpression($expected, $actual);
}
public function testDiffArray()
@ -85,6 +85,6 @@ class DiffTest extends SapphireTest
$expected = "/^Lorem,ipsum *<del>dolor<\/del> *$/";
$actual = Diff::compareHTML($from, $to);
$this->assertRegExp($expected, $actual);
$this->assertMatchesRegularExpression($expected, $actual);
}
}

View File

@ -11,7 +11,7 @@ class ShortcodeParserTest extends SapphireTest
protected $arguments, $contents, $tagName, $parser;
protected $extra = [];
protected function setUp()
protected function setUp(): void
{
ShortcodeParser::get('test')->register('test_shortcode', [$this, 'shortcodeSaver']);
$this->parser = ShortcodeParser::get('test');
@ -19,7 +19,7 @@ class ShortcodeParserTest extends SapphireTest
parent::setUp();
}
protected function tearDown()
protected function tearDown(): void
{
ShortcodeParser::get('test')->unregister('test_shortcode');
@ -244,7 +244,7 @@ class ShortcodeParserTest extends SapphireTest
$this->assertEquals('', $this->parser->parse('[test_shortcode][test_shortcode]'));
}
protected function assertEqualsIgnoringWhitespace($a, $b, $message = null)
protected function assertEqualsIgnoringWhitespace($a, $b, $message = '')
{
$this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message);
}
@ -352,9 +352,9 @@ class ShortcodeParserTest extends SapphireTest
{
$result = $this->parser->parse('<a href="[test_shortcode]?my-string=this&thing=2#my-anchor">Link</a>');
$this->assertContains('my-string=this', $result);
$this->assertContains('thing=2', $result);
$this->assertContains('my-anchor', $result);
$this->assertStringContainsString('my-string=this', $result);
$this->assertStringContainsString('thing=2', $result);
$this->assertStringContainsString('my-anchor', $result);
}
public function testNoParseAttemptIfNoCode()
@ -382,7 +382,7 @@ class ShortcodeParserTest extends SapphireTest
$result = $this->parser->parse('[img]');
$this->assertContains('http://example.com/image.jpg', $result);
$this->assertStringContainsString('http://example.com/image.jpg', $result);
}
// -----------------------------------------------------------------------------------------------------------------

View File

@ -31,7 +31,7 @@ class RequirementsTest extends SapphireTest
static $html_template = '<html><head></head><body></body></html>';
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Director::config()->set('alternate_base_folder', __DIR__ . '/SSViewerTest');
@ -43,7 +43,7 @@ class RequirementsTest extends SapphireTest
$this->oldThemeResourceLoader = ThemeResourceLoader::inst();
}
protected function tearDown()
protected function tearDown(): void
{
ThemeResourceLoader::set_instance($this->oldThemeResourceLoader);
TestAssetStore::reset();
@ -67,14 +67,14 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML(self::$html_template);
$this->assertContains('http://www.mydomain.com/test.js', $html, 'Load external javascript URL');
$this->assertContains('https://www.mysecuredomain.com/test.js', $html, 'Load external secure javascript URL');
$this->assertContains('//scheme-relative.example.com/test.js', $html, 'Load external scheme-relative JS');
$this->assertContains('http://www.mydomain.com:3000/test.js', $html, 'Load external with port');
$this->assertContains('http://www.mydomain.com/test.css', $html, 'Load external CSS URL');
$this->assertContains('https://www.mysecuredomain.com/test.css', $html, 'Load external secure CSS URL');
$this->assertContains('//scheme-relative.example.com/test.css', $html, 'Load scheme-relative CSS URL');
$this->assertContains('http://www.mydomain.com:3000/test.css', $html, 'Load external with port');
$this->assertStringContainsString('http://www.mydomain.com/test.js', $html, 'Load external javascript URL');
$this->assertStringContainsString('https://www.mysecuredomain.com/test.js', $html, 'Load external secure javascript URL');
$this->assertStringContainsString('//scheme-relative.example.com/test.js', $html, 'Load external scheme-relative JS');
$this->assertStringContainsString('http://www.mydomain.com:3000/test.js', $html, 'Load external with port');
$this->assertStringContainsString('http://www.mydomain.com/test.css', $html, 'Load external CSS URL');
$this->assertStringContainsString('https://www.mysecuredomain.com/test.css', $html, 'Load external secure CSS URL');
$this->assertStringContainsString('//scheme-relative.example.com/test.css', $html, 'Load scheme-relative CSS URL');
$this->assertStringContainsString('http://www.mydomain.com:3000/test.css', $html, 'Load external with port');
}
/**
@ -181,11 +181,11 @@ class RequirementsTest extends SapphireTest
);
$backend->javascript('javascript/RequirementsTest_b.js');
$result = $backend->includeInHTML(self::$html_template);
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'#<script type="application/json" src=".*/javascript/RequirementsTest_a.js#',
$result
);
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'#<script type="application/javascript" src=".*/javascript/RequirementsTest_b.js#',
$result
);
@ -204,7 +204,7 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML(self::$html_template);
/* COMBINED JAVASCRIPT FILE IS INCLUDED IN HTML HEADER */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*' . preg_quote($combinedFileName, '/') . '/',
$html,
'combined javascript file is included in html header'
@ -217,31 +217,31 @@ class RequirementsTest extends SapphireTest
);
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertContains(
$this->assertStringContainsString(
"alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
$this->assertContains(
$this->assertStringContainsString(
"alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
/* COMBINED FILES ARE NOT INCLUDED TWICE */
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_b\.js/',
$html,
'combined files are not included twice'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_c\.js/',
$html,
'combined files are not included twice'
);
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*\/RequirementsTest_a\.js/',
$html,
'normal requirements are still included'
@ -255,7 +255,7 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML(self::$html_template);
/* COMBINED JAVASCRIPT FILE IS INCLUDED IN HTML HEADER */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*' . preg_quote($combinedFileName, '/') . '/',
$html,
'combined javascript file is included in html header'
@ -268,24 +268,24 @@ class RequirementsTest extends SapphireTest
);
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertContains(
$this->assertStringContainsString(
"alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
$this->assertContains(
$this->assertStringContainsString(
"alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
/* COMBINED FILES ARE NOT INCLUDED TWICE */
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_b\.js/',
$html,
'combined files are not included twice'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_c\.js/',
$html,
'combined files are not included twice'
@ -305,14 +305,14 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML(false, self::$html_template);
/* ASYNC IS INCLUDED IN SCRIPT TAG */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*' . preg_quote($combinedFileName, '/') . '" async/',
$html,
'async is included in script tag'
);
/* DEFER IS NOT INCLUDED IN SCRIPT TAG */
$this->assertNotContains('defer', $html, 'defer is not included');
$this->assertStringNotContainsString('defer', $html, 'defer is not included');
/* COMBINED JAVASCRIPT FILE EXISTS */
clearstatcache(); // needed to get accurate file_exists() results
@ -322,48 +322,48 @@ class RequirementsTest extends SapphireTest
);
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertContains(
$this->assertStringContainsString(
"alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
$this->assertContains(
$this->assertStringContainsString(
"alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
/* COMBINED FILES ARE NOT INCLUDED TWICE */
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_b\.js/',
$html,
'combined files are not included twice'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_c\.js/',
$html,
'combined files are not included twice'
);
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*\/RequirementsTest_a\.js/',
$html,
'normal requirements are still included'
);
/* NORMAL REQUIREMENTS DON'T HAVE ASYNC/DEFER */
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?m=\d+" async/',
$html,
'normal requirements don\'t have async'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?m=\d+" defer/',
$html,
'normal requirements don\'t have defer'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?m=\d+" async defer/',
$html,
'normal requirements don\'t have async/defer'
@ -379,14 +379,14 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML(self::$html_template);
/* DEFER IS INCLUDED IN SCRIPT TAG */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*' . preg_quote($combinedFileName, '/') . '" defer/',
$html,
'defer is included in script tag'
);
/* ASYNC IS NOT INCLUDED IN SCRIPT TAG */
$this->assertNotContains('async', $html, 'async is not included');
$this->assertStringNotContainsString('async', $html, 'async is not included');
/* COMBINED JAVASCRIPT FILE EXISTS */
clearstatcache(); // needed to get accurate file_exists() results
@ -396,48 +396,48 @@ class RequirementsTest extends SapphireTest
);
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertContains(
$this->assertStringContainsString(
"alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
$this->assertContains(
$this->assertStringContainsString(
"alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
/* COMBINED FILES ARE NOT INCLUDED TWICE */
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_b\.js/',
$html,
'combined files are not included twice'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_c\.js/',
$html,
'combined files are not included twice'
);
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*\/RequirementsTest_a\.js/',
$html,
'normal requirements are still included'
);
/* NORMAL REQUIREMENTS DON'T HAVE ASYNC/DEFER */
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?m=\d+" async/',
$html,
'normal requirements don\'t have async'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?m=\d+" defer/',
$html,
'normal requirements don\'t have defer'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?m=\d+" async defer/',
$html,
'normal requirements don\'t have async/defer'
@ -453,7 +453,7 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML(self::$html_template);
/* ASYNC/DEFER IS INCLUDED IN SCRIPT TAG */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*' . preg_quote($combinedFileName, '/') . '" async="async" defer="defer"/',
$html,
'async and defer are included in script tag'
@ -467,48 +467,48 @@ class RequirementsTest extends SapphireTest
);
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertContains(
$this->assertStringContainsString(
"alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
$this->assertContains(
$this->assertStringContainsString(
"alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content'
);
/* COMBINED FILES ARE NOT INCLUDED TWICE */
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_b\.js/',
$html,
'combined files are not included twice'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_c\.js/',
$html,
'combined files are not included twice'
);
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*\/RequirementsTest_a\.js/',
$html,
'normal requirements are still included'
);
/* NORMAL REQUIREMENTS DON'T HAVE ASYNC/DEFER */
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?m=\d+" async/',
$html,
'normal requirements don\'t have async'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?m=\d+" defer/',
$html,
'normal requirements don\'t have defer'
);
$this->assertNotRegExp(
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?m=\d+" async defer/',
$html,
'normal requirements don\'t have async/defer'
@ -538,12 +538,12 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML(self::$html_template);
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/href=".*\/print\-69ce614\.css/',
$html,
'Print stylesheets have been combined.'
);
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/media="print/',
$html,
'Combined print stylesheet retains the media parameter'
@ -573,7 +573,7 @@ class RequirementsTest extends SapphireTest
);
$html = $backend->includeInHTML(self::$html_template);
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/href=".*\/style\-8011538\.css/',
$html,
'Stylesheets have been combined.'
@ -593,8 +593,8 @@ class RequirementsTest extends SapphireTest
clearstatcache(); // needed to get accurate file_exists() results
$html = $backend->includeInHTML(self::$html_template);
$this->assertFileNotExists($combinedFilePath);
$this->assertNotRegExp(
$this->assertFileDoesNotExist($combinedFilePath);
$this->assertDoesNotMatchRegularExpression(
'/src=".*\/RequirementsTest_bc\.js/',
$html,
'blocked combined files are not included'
@ -609,7 +609,7 @@ class RequirementsTest extends SapphireTest
clearstatcache(); // needed to get accurate file_exists() results
$backend->includeInHTML(self::$html_template);
$this->assertFileExists($combinedFilePath2);
$this->assertNotContains(
$this->assertStringNotContainsString(
"alert('b')",
file_get_contents($combinedFilePath2),
'blocked uncombined files are not included'
@ -621,7 +621,7 @@ class RequirementsTest extends SapphireTest
clearstatcache(); // needed to get accurate file_exists() results
// Exception generated from including invalid file
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf(
"Requirements_Backend::combine_files(): Already included file(s) %s in combined file '%s'",
'javascript/RequirementsTest_c.js',
@ -650,14 +650,14 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML(self::$html_template);
/* Javascript has correct path */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/src=".*\/RequirementsTest_a\.js\?test=1&amp;test=2&amp;test=3&amp;m=\d\d+/',
$html,
'javascript has correct path'
);
/* CSS has correct path */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/href=".*\/RequirementsTest_a\.css\?test=1&amp;test=2&amp;test=3&amp;m=\d\d+/',
$html,
'css has correct path'
@ -825,12 +825,12 @@ class RequirementsTest extends SapphireTest
$backend->setWriteJavascriptToBody(false);
$html = $backend->includeInHTML($template);
$this->assertContains('<head><script', $html);
$this->assertStringContainsString('<head><script', $html);
$backend->setWriteJavascriptToBody(true);
$html = $backend->includeInHTML($template);
$this->assertNotContains('<head><script', $html);
$this->assertContains("</script>\n</body>", $html);
$this->assertStringNotContainsString('<head><script', $html);
$this->assertStringContainsString("</script>\n</body>", $html);
}
public function testIncludedJsIsNotCommentedOut()
@ -843,7 +843,7 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML($template);
//wiping out commented-out html
$html = preg_replace('/<!--(.*)-->/Uis', '', $html);
$this->assertContains("RequirementsTest_a.js", $html);
$this->assertStringContainsString("RequirementsTest_a.js", $html);
}
public function testCommentedOutScriptTagIsIgnored()
@ -952,18 +952,18 @@ EOS
$urlGenerator->setNonceStyle('mtime');
$html = $backend->includeInHTML($template);
$this->assertRegExp('/RequirementsTest_a\.js\?m=[\d]*"/', $html);
$this->assertRegExp('/RequirementsTest_b\.js\?foo=bar&amp;bla=blubb&amp;m=[\d]*"/', $html);
$this->assertRegExp('/RequirementsTest_a\.css\?m=[\d]*"/', $html);
$this->assertRegExp('/RequirementsTest_b\.css\?foo=bar&amp;bla=blubb&amp;m=[\d]*"/', $html);
$this->assertMatchesRegularExpression('/RequirementsTest_a\.js\?m=[\d]*"/', $html);
$this->assertMatchesRegularExpression('/RequirementsTest_b\.js\?foo=bar&amp;bla=blubb&amp;m=[\d]*"/', $html);
$this->assertMatchesRegularExpression('/RequirementsTest_a\.css\?m=[\d]*"/', $html);
$this->assertMatchesRegularExpression('/RequirementsTest_b\.css\?foo=bar&amp;bla=blubb&amp;m=[\d]*"/', $html);
$urlGenerator->setNonceStyle(null);
$html = $backend->includeInHTML($template);
$this->assertNotContains('RequirementsTest_a.js=', $html);
$this->assertNotRegExp('/RequirementsTest_a\.js\?m=[\d]*"/', $html);
$this->assertNotRegExp('/RequirementsTest_b\.js\?foo=bar&amp;bla=blubb&amp;m=[\d]*"/', $html);
$this->assertNotRegExp('/RequirementsTest_a\.css\?m=[\d]*"/', $html);
$this->assertNotRegExp('/RequirementsTest_b\.css\?foo=bar&amp;bla=blubb&amp;m=[\d]*"/', $html);
$this->assertStringNotContainsString('RequirementsTest_a.js=', $html);
$this->assertDoesNotMatchRegularExpression('/RequirementsTest_a\.js\?m=[\d]*"/', $html);
$this->assertDoesNotMatchRegularExpression('/RequirementsTest_b\.js\?foo=bar&amp;bla=blubb&amp;m=[\d]*"/', $html);
$this->assertDoesNotMatchRegularExpression('/RequirementsTest_a\.css\?m=[\d]*"/', $html);
$this->assertDoesNotMatchRegularExpression('/RequirementsTest_b\.css\?foo=bar&amp;bla=blubb&amp;m=[\d]*"/', $html);
}
/**
@ -997,9 +997,9 @@ EOS
$backend->getProvidedScripts()
);
$html = $backend->includeInHTML($template);
$this->assertRegExp('/src=".*\/RequirementsTest_a\.js/', $html);
$this->assertRegExp('/src=".*\/RequirementsTest_b\.js/', $html);
$this->assertNotRegExp('/src=".*\/RequirementsTest_c\.js/', $html);
$this->assertMatchesRegularExpression('/src=".*\/RequirementsTest_a\.js/', $html);
$this->assertMatchesRegularExpression('/src=".*\/RequirementsTest_b\.js/', $html);
$this->assertDoesNotMatchRegularExpression('/src=".*\/RequirementsTest_c\.js/', $html);
// Test that provided files block subsequent combined files
$backend = Injector::inst()->create(Requirements_Backend::class);
@ -1022,10 +1022,10 @@ EOS
$backend->getProvidedScripts()
);
$html = $backend->includeInHTML($template);
$this->assertRegExp('/src=".*\/combined_a/', $html);
$this->assertRegExp('/src=".*\/RequirementsTest_b\.js/', $html);
$this->assertNotRegExp('/src=".*\/combined_c/', $html);
$this->assertNotRegExp('/src=".*\/RequirementsTest_c\.js/', $html);
$this->assertMatchesRegularExpression('/src=".*\/combined_a/', $html);
$this->assertMatchesRegularExpression('/src=".*\/RequirementsTest_b\.js/', $html);
$this->assertDoesNotMatchRegularExpression('/src=".*\/combined_c/', $html);
$this->assertDoesNotMatchRegularExpression('/src=".*\/RequirementsTest_c\.js/', $html);
}
/**
@ -1198,14 +1198,14 @@ EOS
$html = $backend->includeInHTML(self::$html_template);
/* Javascript has correct attributes */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'#<script type="application/javascript" src=".*/javascript/RequirementsTest_a.js.*" integrity="abc" crossorigin="use-credentials"#',
$html,
'javascript has correct sri attributes'
);
/* CSS has correct attributes */
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'#<link .*href=".*/RequirementsTest_a\.css.*" integrity="def" crossorigin="anonymous"#',
$html,
'css has correct sri attributes'

View File

@ -9,6 +9,7 @@ use SilverStripe\Versioned\Versioned;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Director;
use SilverStripe\View\SSTemplateParseException;
use SilverStripe\View\SSViewer;
use Symfony\Component\Cache\Simple\FilesystemCache;
use Symfony\Component\Cache\Simple\NullCache;
@ -422,11 +423,9 @@ class SSViewerCacheBlockTest extends SapphireTest
$this->assertNotNull($this->_runtemplate('<% cached %><% with Foo %>$Bar<% end_with %><% end_cached %>'));
}
/**
* @expectedException \SilverStripe\View\SSTemplateParseException
*/
public function testErrorMessageForCachedWithinControlWithinCached()
{
$this->expectException(SSTemplateParseException::class);
$this->_reset(true);
$this->_runtemplate(
'<% cached %><% with Foo %><% cached %>$Bar<% end_cached %><% end_with %><% end_cached %>'
@ -443,20 +442,16 @@ class SSViewerCacheBlockTest extends SapphireTest
);
}
/**
* @expectedException \SilverStripe\View\SSTemplateParseException
*/
public function testErrorMessageForCachedWithinIf()
{
$this->expectException(SSTemplateParseException::class);
$this->_reset(true);
$this->_runtemplate('<% cached %><% if Foo %><% cached %>$Bar<% end_cached %><% end_if %><% end_cached %>');
}
/**
* @expectedException \SilverStripe\View\SSTemplateParseException
*/
public function testErrorMessageForInvalidConditional()
{
$this->expectException(SSTemplateParseException::class);
$this->_reset(true);
$this->_runtemplate('<% cached Foo if %>$Bar<% end_cached %>');
}

View File

@ -4,7 +4,7 @@ namespace SilverStripe\View\Tests;
use Exception;
use InvalidArgumentException;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use Silverstripe\Assets\Dev\TestAssetStore;
use SilverStripe\Control\ContentNegotiator;
use SilverStripe\Control\Controller;
@ -25,6 +25,7 @@ use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use SilverStripe\View\Requirements_Backend;
use SilverStripe\View\Requirements_Minifier;
use SilverStripe\View\SSTemplateParseException;
use SilverStripe\View\SSTemplateParser;
use SilverStripe\View\SSViewer;
use SilverStripe\View\SSViewer_FromString;
@ -49,7 +50,7 @@ class SSViewerTest extends SapphireTest
SSViewerTest\TestObject::class,
];
protected function setUp()
protected function setUp(): void
{
parent::setUp();
SSViewer::config()->update('source_file_comments', false);
@ -58,7 +59,7 @@ class SSViewerTest extends SapphireTest
$this->oldServer = $_SERVER;
}
protected function tearDown()
protected function tearDown(): void
{
$_SERVER = $this->oldServer;
TestAssetStore::reset();
@ -221,7 +222,7 @@ class SSViewerTest extends SapphireTest
public function testRequirements()
{
/** @var Requirements_Backend|PHPUnit_Framework_MockObject_MockObject $requirements */
/** @var Requirements_Backend|MockObject $requirements */
$requirements = $this
->getMockBuilder(Requirements_Backend::class)
->setMethods(["javascript", "css"])
@ -260,14 +261,14 @@ class SSViewerTest extends SapphireTest
$testBackend->processCombinedFiles();
$js = array_keys($testBackend->getJavascript());
$combinedTestFilePath = Director::publicFolder() . reset($js);
$this->assertContains('_combinedfiles/testRequirementsCombine-4c0e97a.js', $combinedTestFilePath);
$this->assertStringContainsString('_combinedfiles/testRequirementsCombine-4c0e97a.js', $combinedTestFilePath);
// and make sure the combined content matches the input content, i.e. no loss of functionality
if (!file_exists($combinedTestFilePath)) {
$this->fail('No combined file was created at expected path: ' . $combinedTestFilePath);
}
$combinedTestFileContents = file_get_contents($combinedTestFilePath);
$this->assertContains($jsFileContents, $combinedTestFileContents);
$this->assertStringContainsString($jsFileContents, $combinedTestFileContents);
}
public function testRequirementsMinification()
@ -302,7 +303,7 @@ class SSViewerTest extends SapphireTest
$testBackend->processCombinedFiles();
$this->expectException(Exception::class);
$this->expectExceptionMessageRegExp('/^Cannot minify files without a minification service defined./');
$this->expectExceptionMessageMatches('/^Cannot minify files without a minification service defined./');
$testBackend->setMinifyCombinedFiles(true);
$testBackend->setMinifier(null);
@ -377,12 +378,10 @@ SS;
);
}
/**
* @expectedException SilverStripe\View\SSTemplateParseException
* @expectedExceptionMessageRegExp /Malformed bracket injection {\$Value(.*)/
*/
public function testBasicInjectionMismatchedBrackets()
{
$this->expectException(SSTemplateParseException::class);
$this->expectExceptionMessageMatches('/Malformed bracket injection {\$Value(.*)/');
$this->render('A {$Value here');
$this->fail("Parser didn't error when encountering mismatched brackets in an injection");
}
@ -954,7 +953,7 @@ after'
<head><% base_tag %></head>
<body><p>test</p><body>
</html>';
$this->assertRegExp('/<head><base href=".*" \/><\/head>/', $this->render($tmpl1));
$this->assertMatchesRegularExpression('/<head><base href=".*" \/><\/head>/', $this->render($tmpl1));
// HTML4 and 5 will only have it for IE
$tmpl2 = '<!DOCTYPE html>
@ -962,7 +961,7 @@ after'
<head><% base_tag %></head>
<body><p>test</p><body>
</html>';
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/',
$this->render($tmpl2)
);
@ -973,7 +972,7 @@ after'
<head><% base_tag %></head>
<body><p>test</p><body>
</html>';
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/',
$this->render($tmpl3)
);
@ -983,14 +982,14 @@ after'
$response = new HTTPResponse($this->render($tmpl1));
$negotiator->html($response);
$this->assertRegExp(
$this->assertMatchesRegularExpression(
'/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/',
$response->getBody()
);
$response = new HTTPResponse($this->render($tmpl1));
$negotiator->xhtml($response);
$this->assertRegExp('/<head><base href=".*" \/><\/head>/', $response->getBody());
$this->assertMatchesRegularExpression('/<head><base href=".*" \/><\/head>/', $response->getBody());
}
public function testIncludeWithArguments()
@ -1707,7 +1706,7 @@ after'
);
// Let's throw something random in there.
$this->expectException(InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);
SSViewer::get_templates_by_class(null);
}
);
@ -1753,23 +1752,23 @@ after'
'<a class="external-inserted" href="http://google.com#anchor">ExternalInsertedLink</a>'
);
$result = $tmpl->process($obj);
$this->assertContains(
$this->assertStringContainsString(
'<a class="inserted" href="' . $base . '#anchor">InsertedLink</a>',
$result
);
$this->assertContains(
$this->assertStringContainsString(
'<a class="external-inserted" href="http://google.com#anchor">ExternalInsertedLink</a>',
$result
);
$this->assertContains(
$this->assertStringContainsString(
'<a class="inline" href="' . $base . '#anchor">InlineLink</a>',
$result
);
$this->assertContains(
$this->assertStringContainsString(
'<a class="external-inline" href="http://google.com#anchor">ExternalInlineLink</a>',
$result
);
$this->assertContains(
$this->assertStringContainsString(
'<svg><use xlink:href="#sprite"></use></svg>',
$result,
'SSTemplateParser should only rewrite anchor hrefs'
@ -1808,13 +1807,8 @@ after'
$code = <<<'EOC'
<a class="inserted" href="<?php echo \SilverStripe\Core\Convert::raw2att(preg_replace("/^(\/)+/", "/", $_SERVER['REQUEST_URI'])); ?>#anchor">InsertedLink</a>
EOC;
$this->assertContains($code, $result);
// TODO Fix inline links in PHP mode
// $this->assertContains(
// '<a class="inline" href="<?php echo str_replace(',
// $result
// );
$this->assertContains(
$this->assertStringContainsString($code, $result);
$this->assertStringContainsString(
'<svg><use xlink:href="#sprite"></use></svg>',
$result,
'SSTemplateParser should only rewrite anchor hrefs'

View File

@ -215,7 +215,7 @@ class ViewableDataTest extends SapphireTest
SSViewer::set_themes($themes);
$data = new ViewableData();
$this->assertContains(
$this->assertStringContainsString(
'tests/php/View/ViewableDataTest/testtheme',
$data->ThemeDir()
);

View File

@ -46,7 +46,7 @@ class YamlReaderTest extends SapphireTest
$path = __DIR__ . '/i18nTest/_fakewebroot/i18ntestmodule/lang/en_corrupt.yml';
$this->expectException(InvalidResourceException::class);
$regex_path = str_replace('.', '\.', $path);
$this->expectExceptionMessageRegExp('@^Error parsing YAML, invalid file "' . $regex_path . '"\. Message: ([\w ].*) line 5 @');
$this->expectExceptionMessageMatches('@^Error parsing YAML, invalid file "' . $regex_path . '"\. Message: ([\w ].*) line 5 @');
$reader = new YamlReader();
$reader->read('en', $path);
}

View File

@ -17,13 +17,13 @@ class i18nTest extends SapphireTest
{
use i18nTestManifest;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->setupManifest();
}
protected function tearDown()
protected function tearDown(): void
{
$this->tearDownManifest();
parent::tearDown();
@ -173,11 +173,11 @@ class i18nTest extends SapphireTest
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData([
'TestProperty' => 'TestPropertyValue'
])));
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("Layout Template\n"),
$parsedHtml
);
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("Layout Template no namespace\n"),
$parsedHtml
);
@ -202,42 +202,42 @@ class i18nTest extends SapphireTest
i18n::set_locale('de_DE');
$viewer = new SSViewer('i18nTestModule');
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData(['TestProperty' => 'TestPropertyValue'])));
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("TRANS Main Template\n"),
$parsedHtml
);
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("TRANS Layout Template\n"),
$parsedHtml
);
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("TRANS Layout Template no namespace\n"),
$parsedHtml
);
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("TRANS My replacement: TestPropertyValue\n"),
$parsedHtml
);
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("TRANS Include Entity with Namespace\n"),
$parsedHtml
);
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("TRANS Include Entity without Namespace\n"),
$parsedHtml
);
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("TRANS My include replacement: TestPropertyValue\n"),
$parsedHtml
);
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("TRANS My include replacement no namespace: TestPropertyValue\n"),
$parsedHtml
);
// Check plurals
$this->assertContains('Single: An item', $parsedHtml);
$this->assertContains('Multiple: 4 items', $parsedHtml);
$this->assertContains('None: 0 items', $parsedHtml);
$this->assertStringContainsString('Single: An item', $parsedHtml);
$this->assertStringContainsString('Multiple: 4 items', $parsedHtml);
$this->assertStringContainsString('None: 0 items', $parsedHtml);
i18n::set_locale($oldLocale);
}
@ -264,7 +264,7 @@ class i18nTest extends SapphireTest
$default,
["name"=>"Mark", "greeting"=>"welcome", "goodbye"=>"bye"]
);
$this->assertContains(
$this->assertStringContainsString(
"Hello Mark welcome. But it is late, bye",
$translated,
"Testing fallback to the translation default (but using the injection array)"
@ -276,7 +276,7 @@ class i18nTest extends SapphireTest
$default,
["name"=>"Paul", "greeting"=>"good you are here", "goodbye"=>"see you"]
);
$this->assertContains(
$this->assertStringContainsString(
"TRANS Hello Paul good you are here. But it is late, see you",
$translated,
"Testing entity, default string and injection array"
@ -289,7 +289,7 @@ class i18nTest extends SapphireTest
"New context (this should be ignored)",
["name"=>"Steffen", "greeting"=>"willkommen", "goodbye"=>"wiedersehen"]
);
$this->assertContains(
$this->assertStringContainsString(
"TRANS Hello Steffen willkommen. But it is late, wiedersehen",
$translated,
"Full test of translation, using default, context and injection array"
@ -326,20 +326,20 @@ class i18nTest extends SapphireTest
$viewer = new SSViewer('i18nTestModule');
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData(['TestProperty' => 'TestPropertyValue'])));
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("Hello Mark welcome. But it is late, bye\n"),
$parsedHtml,
"Testing fallback to the translation default (but using the injection array)"
);
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os("TRANS Hello Paul good you are here. But it is late, see you\n"),
$parsedHtml,
"Testing entity, default string and injection array"
);
//test injected calls
$this->assertContains(
$this->assertStringContainsString(
Convert::nl2os(
"TRANS Hello " . Director::absoluteBaseURL() . " " . i18n::get_locale() . ". But it is late, global calls\n"
),

View File

@ -2,7 +2,6 @@
namespace SilverStripe\i18n\Tests;
use PHPUnit_Framework_Error_Notice;
use SilverStripe\Assets\Filesystem;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Dev\SapphireTest;
@ -20,7 +19,7 @@ class i18nTextCollectorTest extends SapphireTest
*/
protected $alternateBaseSavePath = null;
protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->setupManifest();
@ -29,7 +28,7 @@ class i18nTextCollectorTest extends SapphireTest
Filesystem::makeFolder($this->alternateBaseSavePath);
}
protected function tearDown()
protected function tearDown(): void
{
if (is_dir($this->alternateBaseSavePath)) {
Filesystem::removeFolder($this->alternateBaseSavePath);
@ -114,8 +113,8 @@ SS;
// Test warning is raised on empty default
$c->setWarnOnEmptyDefault(true);
$this->expectException(PHPUnit_Framework_Error_Notice::class);
$this->expectExceptionMessage('Missing localisation default for key i18nTestModule.INJECTIONS_3');
$this->expectNotice();
$this->expectNoticeMessage('Missing localisation default for key i18nTestModule.INJECTIONS_3');
$c->collectFromTemplate($html, null, $mymodule);
}
@ -189,8 +188,8 @@ SS;
// Test warning is raised on empty default
$c->setWarnOnEmptyDefault(true);
$this->expectException(PHPUnit_Framework_Error_Notice::class);
$this->expectExceptionMessage('Missing localisation default for key Test.PRIOANDCOMMENT');
$this->expectNotice();
$this->expectNoticeMessage('Missing localisation default for key Test.PRIOANDCOMMENT');
$c->collectFromTemplate($html, 'Test', $mymodule);
}
@ -425,8 +424,8 @@ PHP;
$this->assertEquals($expectedArray, $collectedTranslatables);
// Test warning is raised on empty default
$this->expectException(PHPUnit_Framework_Error_Notice::class);
$this->expectExceptionMessage('Missing localisation default for key i18nTestModule.INJECTIONS4');
$this->expectNotice();
$this->expectNoticeMessage('Missing localisation default for key i18nTestModule.INJECTIONS4');
$php = <<<PHP
_t('i18nTestModule.INJECTIONS4', ["name"=>"Cat", "greeting"=>"meow", "goodbye"=>"meow"]);
@ -547,27 +546,27 @@ PHP;
);
$moduleLangFileContent = file_get_contents($moduleLangFile);
$this->assertContains(
$this->assertStringContainsString(
" ADDITION: Addition\n",
$moduleLangFileContent
);
$this->assertContains(
$this->assertStringContainsString(
" ENTITY: 'Entity with \"Double Quotes\"'\n",
$moduleLangFileContent
);
$this->assertContains(
$this->assertStringContainsString(
" MAINTEMPLATE: 'Main Template'\n",
$moduleLangFileContent
);
$this->assertContains(
$this->assertStringContainsString(
" OTHERENTITY: 'Other Entity'\n",
$moduleLangFileContent
);
$this->assertContains(
$this->assertStringContainsString(
" WITHNAMESPACE: 'Include Entity with Namespace'\n",
$moduleLangFileContent
);
$this->assertContains(
$this->assertStringContainsString(
" NONAMESPACE: 'Include Entity without Namespace'\n",
$moduleLangFileContent
);
@ -579,11 +578,11 @@ PHP;
'Master language file can be written to modules /lang folder'
);
$otherModuleLangFileContent = file_get_contents($otherModuleLangFile);
$this->assertContains(
$this->assertStringContainsString(
" ENTITY: 'Other Module Entity'\n",
$otherModuleLangFileContent
);
$this->assertContains(
$this->assertStringContainsString(
" MAINTEMPLATE: 'Main Template Other Module'\n",
$otherModuleLangFileContent
);