2008-05-15 09:15:33 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\BasicAuth;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\SecurityToken;
|
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
/**
|
2012-03-24 04:38:57 +01:00
|
|
|
* SilverStripe-specific testing object designed to support functional testing of your web app. It simulates get/post
|
2008-05-15 09:15:33 +02:00
|
|
|
* requests, form submission, and can validate resulting HTML, looking up content by CSS selector.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2008-05-15 09:15:33 +02:00
|
|
|
* The example below shows how it works.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2008-05-15 09:15:33 +02:00
|
|
|
* <code>
|
2012-09-19 12:07:39 +02:00
|
|
|
* public function testMyForm() {
|
2012-12-08 12:20:20 +01:00
|
|
|
* // Visit a URL
|
|
|
|
* $this->get("your/url");
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2012-12-08 12:20:20 +01:00
|
|
|
* // Submit a form on the page that you get in response
|
2015-03-04 06:38:43 +01:00
|
|
|
* $this->submitForm("MyForm_ID", "action_dologin", array("Email" => "invalid email ^&*&^"));
|
2008-05-15 09:15:33 +02:00
|
|
|
*
|
2012-12-08 12:20:20 +01:00
|
|
|
* // Validate the content that is returned
|
|
|
|
* $this->assertExactMatchBySelector("#MyForm_ID p.error", array("That email address is invalid."));
|
2014-07-28 23:24:40 +02:00
|
|
|
* }
|
2008-05-15 09:15:33 +02:00
|
|
|
* </code>
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-06-15 15:33:53 +02:00
|
|
|
* @subpackage testing
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
|
|
|
class FunctionalTest extends SapphireTest {
|
2008-05-26 08:21:30 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
|
|
|
* @var bool
|
2008-05-26 08:21:30 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $disable_themes = false;
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-26 08:21:30 +02:00
|
|
|
/**
|
|
|
|
* Set this to true on your sub-class to use the draft site by default for every test in this class.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
|
|
|
* @var bool
|
2008-05-26 08:21:30 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $use_draft_site = false;
|
2014-07-28 23:24:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TestSession
|
|
|
|
*/
|
2008-05-15 09:15:33 +02:00
|
|
|
protected $mainSession = null;
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
/**
|
|
|
|
* CSSContentParser for the most recently requested page.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2014-04-09 01:41:25 +02:00
|
|
|
* @var CSSContentParser
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
|
|
|
protected $cssParser = null;
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-08-12 01:04:01 +02:00
|
|
|
/**
|
|
|
|
* If this is true, then 30x Location headers will be automatically followed.
|
2012-09-26 23:34:00 +02:00
|
|
|
* If not, then you will have to manaully call $this->mainSession->followRedirection() to follow them.
|
|
|
|
* However, this will let you inspect the intermediary headers
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
|
|
|
* @var bool
|
2008-08-12 01:04:01 +02:00
|
|
|
*/
|
|
|
|
protected $autoFollowRedirection = true;
|
2014-02-07 04:38:02 +01:00
|
|
|
|
|
|
|
/**
|
2014-07-28 23:24:40 +02:00
|
|
|
* @var string
|
2014-02-07 04:38:02 +01:00
|
|
|
*/
|
|
|
|
protected $originalTheme = null;
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-26 08:21:30 +02:00
|
|
|
/**
|
|
|
|
* Returns the {@link Session} object for this test
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
|
|
|
* @return Session
|
2008-05-26 08:21:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function session() {
|
2008-05-26 08:21:30 +02:00
|
|
|
return $this->mainSession->session();
|
|
|
|
}
|
2008-05-15 09:15:33 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2011-09-26 05:08:41 +02:00
|
|
|
// Skip calling FunctionalTest directly.
|
2015-07-16 12:12:15 +02:00
|
|
|
if(get_class($this) == "FunctionalTest") {
|
|
|
|
$this->markTestSkipped(sprintf('Skipping %s ', get_class($this)));
|
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
parent::setUp();
|
|
|
|
$this->mainSession = new TestSession();
|
2008-05-26 08:21:30 +02:00
|
|
|
|
|
|
|
// Disable theme, if necessary
|
2014-02-07 04:38:02 +01:00
|
|
|
if(static::get_disable_themes()) {
|
|
|
|
$this->originalTheme = Config::inst()->get('SSViewer', 'theme');
|
|
|
|
Config::inst()->update('SSViewer', 'theme', null);
|
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-26 08:21:30 +02:00
|
|
|
// Switch to draft site, if necessary
|
2013-03-21 19:48:54 +01:00
|
|
|
if(static::get_use_draft_site()) {
|
2008-05-26 08:21:30 +02:00
|
|
|
$this->useDraftSite();
|
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
// 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);
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
SecurityToken::disable();
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
2008-05-26 08:21:30 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDown() {
|
2010-12-05 09:22:57 +01:00
|
|
|
SecurityToken::enable();
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
parent::tearDown();
|
2008-12-04 23:38:32 +01:00
|
|
|
unset($this->mainSession);
|
2014-02-07 04:38:02 +01:00
|
|
|
|
|
|
|
if(static::get_disable_themes()) {
|
|
|
|
Config::inst()->update('SSViewer', 'theme', $this->originalTheme);
|
|
|
|
}
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 04:45:44 +02:00
|
|
|
/**
|
|
|
|
* 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', 'alternate_base_url');
|
|
|
|
Config::inst()->update('Director', 'alternate_base_url', $url);
|
|
|
|
$callback($this);
|
|
|
|
Config::inst()->update('Director', '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', 'alternate_base_folder');
|
|
|
|
Config::inst()->update('Director', 'alternate_base_folder', $folder);
|
|
|
|
$callback($this);
|
|
|
|
Config::inst()->update('Director', 'alternate_base_folder', $oldFolder);
|
|
|
|
}
|
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
/**
|
|
|
|
* Submit a get request
|
2009-12-16 06:42:43 +01:00
|
|
|
* @uses Director::test()
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param Session $session
|
|
|
|
* @param array $headers
|
|
|
|
* @param array $cookies
|
|
|
|
* @return SS_HTTPResponse
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function get($url, $session = null, $headers = null, $cookies = null) {
|
2008-05-15 09:15:33 +02:00
|
|
|
$this->cssParser = null;
|
2009-12-16 06:42:43 +01:00
|
|
|
$response = $this->mainSession->get($url, $session, $headers, $cookies);
|
2012-09-26 23:34:00 +02:00
|
|
|
if($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) {
|
|
|
|
$response = $this->mainSession->followRedirection();
|
|
|
|
}
|
2008-05-26 08:21:30 +02:00
|
|
|
return $response;
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Submit a post request
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2009-12-16 06:42:43 +01:00
|
|
|
* @uses Director::test()
|
2014-07-28 23:24:40 +02:00
|
|
|
* @param string $url
|
|
|
|
* @param array $data
|
|
|
|
* @param array $headers
|
|
|
|
* @param Session $session
|
|
|
|
* @param string $body
|
|
|
|
* @param array $cookies
|
|
|
|
* @return SS_HTTPResponse
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null) {
|
2008-05-15 09:15:33 +02:00
|
|
|
$this->cssParser = null;
|
2009-12-16 06:42:43 +01:00
|
|
|
$response = $this->mainSession->post($url, $data, $headers, $session, $body, $cookies);
|
2012-09-26 23:34:00 +02:00
|
|
|
if($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) {
|
|
|
|
$response = $this->mainSession->followRedirection();
|
|
|
|
}
|
2008-05-26 08:21:30 +02:00
|
|
|
return $response;
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
/**
|
|
|
|
* Submit the form with the given HTML ID, filling it out with the given data.
|
2010-11-24 07:23:52 +01:00
|
|
|
* Acts on the most recent response.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2010-11-24 07:23:52 +01:00
|
|
|
* Any data parameters have to be present in the form, with exact form field name
|
|
|
|
* and values, otherwise they are removed from the submission.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2010-11-24 07:23:52 +01:00
|
|
|
* 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')
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2010-11-24 07:23:52 +01:00
|
|
|
* @see http://www.simpletest.org/en/form_testing_documentation.html
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
|
|
|
* @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.
|
2010-11-24 07:23:52 +01:00
|
|
|
* @return SS_HTTPResponse
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function submitForm($formID, $button = null, $data = array()) {
|
2008-05-15 09:15:33 +02:00
|
|
|
$this->cssParser = null;
|
2008-05-26 08:21:30 +02:00
|
|
|
$response = $this->mainSession->submitForm($formID, $button, $data);
|
2012-09-26 23:34:00 +02:00
|
|
|
if($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) {
|
|
|
|
$response = $this->mainSession->followRedirection();
|
|
|
|
}
|
2008-05-26 08:21:30 +02:00
|
|
|
return $response;
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
/**
|
|
|
|
* Return the most recent content
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function content() {
|
2008-05-15 09:15:33 +02:00
|
|
|
return $this->mainSession->lastContent();
|
|
|
|
}
|
2009-12-16 06:40:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find an attribute in a SimpleXMLElement object by name.
|
2014-07-28 23:24:40 +02:00
|
|
|
* @param SimpleXMLElement $object
|
2009-12-16 06:40:27 +01:00
|
|
|
* @param string $attribute Name of attribute to find
|
|
|
|
* @return SimpleXMLElement object of the attribute
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function findAttribute($object, $attribute) {
|
2009-12-16 06:40:27 +01:00
|
|
|
$found = false;
|
|
|
|
foreach($object->attributes() as $a => $b) {
|
|
|
|
if($a == $attribute) {
|
|
|
|
$found = $b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
/**
|
|
|
|
* Return a CSSContentParser for the most recent content.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2014-04-09 01:41:25 +02:00
|
|
|
* @return CSSContentParser
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function cssParser() {
|
2008-05-15 09:15:33 +02:00
|
|
|
if(!$this->cssParser) $this->cssParser = new CSSContentParser($this->mainSession->lastContent());
|
|
|
|
return $this->cssParser;
|
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2008-11-08 01:03:50 +01:00
|
|
|
* will be examined. The assertion fails if one of the expectedMatches fails to appear.
|
2008-05-15 09:15:33 +02:00
|
|
|
*
|
|
|
|
* Note: characters are stripped from the content; make sure that your assertions take this into account.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2008-11-08 01:03:50 +01:00
|
|
|
* @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
|
|
|
|
* @throws PHPUnit_Framework_AssertionFailedError
|
|
|
|
* @return boolean
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function assertPartialMatchBySelector($selector, $expectedMatches) {
|
2008-11-08 01:03:50 +01:00
|
|
|
if(is_string($expectedMatches)) $expectedMatches = array($expectedMatches);
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
$items = $this->cssParser()->getBySelector($selector);
|
2008-08-13 05:40:56 +02:00
|
|
|
|
|
|
|
$actuals = array();
|
|
|
|
if($items) foreach($items as $item) $actuals[trim(preg_replace("/[ \n\r\t]+/", " ", $item. ''))] = true;
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
foreach($expectedMatches as $match) {
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
isset($actuals[$match]),
|
2012-12-08 12:20:20 +01:00
|
|
|
"Failed asserting the CSS selector '$selector' has a partial match to the expected elements:\n'"
|
2014-07-28 23:24:40 +02:00
|
|
|
. implode("'\n'", $expectedMatches) . "'\n\n"
|
2012-11-23 14:55:19 +01:00
|
|
|
. "Instead the following elements were found:\n'" . implode("'\n'", array_keys($actuals)) . "'"
|
|
|
|
);
|
|
|
|
return false;
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-11-08 01:03:50 +01:00
|
|
|
return true;
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2008-11-08 01:03:50 +01:00
|
|
|
* will be examined. The assertion fails if one of the expectedMatches fails to appear.
|
2008-05-15 09:15:33 +02:00
|
|
|
*
|
|
|
|
* Note: characters are stripped from the content; make sure that your assertions take this into account.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2008-11-08 01:03:50 +01:00
|
|
|
* @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
|
|
|
|
* @throws PHPUnit_Framework_AssertionFailedError
|
|
|
|
* @return boolean
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function assertExactMatchBySelector($selector, $expectedMatches) {
|
2008-11-08 01:03:50 +01:00
|
|
|
if(is_string($expectedMatches)) $expectedMatches = array($expectedMatches);
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
$items = $this->cssParser()->getBySelector($selector);
|
2008-08-13 05:40:56 +02:00
|
|
|
|
|
|
|
$actuals = array();
|
|
|
|
if($items) foreach($items as $item) $actuals[] = trim(preg_replace("/[ \n\r\t]+/", " ", $item. ''));
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$expectedMatches == $actuals,
|
|
|
|
"Failed asserting the CSS selector '$selector' has an exact match to the expected elements:\n'"
|
2014-07-28 23:24:40 +02:00
|
|
|
. implode("'\n'", $expectedMatches) . "'\n\n"
|
2012-11-23 14:55:19 +01:00
|
|
|
. "Instead the following elements were found:\n'" . implode("'\n'", $actuals) . "'"
|
|
|
|
);
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-11-08 01:03:50 +01:00
|
|
|
return true;
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2008-11-08 01:03:50 +01:00
|
|
|
* will be examined. The assertion fails if one of the expectedMatches fails to appear.
|
2008-05-15 09:15:33 +02:00
|
|
|
*
|
|
|
|
* Note: characters are stripped from the content; make sure that your assertions take this into account.
|
2008-11-08 01:03:50 +01:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @throws PHPUnit_Framework_AssertionFailedError
|
|
|
|
* @return boolean
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function assertPartialHTMLMatchBySelector($selector, $expectedMatches) {
|
2008-11-08 01:03:50 +01:00
|
|
|
if(is_string($expectedMatches)) $expectedMatches = array($expectedMatches);
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
$items = $this->cssParser()->getBySelector($selector);
|
2008-08-13 05:40:56 +02:00
|
|
|
|
|
|
|
$actuals = array();
|
|
|
|
if($items) foreach($items as $item) $actuals[$item->asXML()] = true;
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-15 09:15:33 +02:00
|
|
|
foreach($expectedMatches as $match) {
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
isset($actuals[$match]),
|
|
|
|
"Failed asserting the CSS selector '$selector' has a partial match to the expected elements:\n'"
|
2014-07-28 23:24:40 +02:00
|
|
|
. implode("'\n'", $expectedMatches) . "'\n\n"
|
2012-11-23 14:55:19 +01:00
|
|
|
. "Instead the following elements were found:\n'" . implode("'\n'", array_keys($actuals)) . "'"
|
|
|
|
);
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-11-08 01:03:50 +01:00
|
|
|
return true;
|
2008-05-15 09:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2008-11-08 01:03:50 +01:00
|
|
|
* will be examined. The assertion fails if one of the expectedMatches fails to appear.
|
2008-05-15 09:15:33 +02:00
|
|
|
*
|
|
|
|
* Note: characters are stripped from the content; make sure that your assertions take this into account.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
2008-11-08 01:03:50 +01:00
|
|
|
* @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
|
|
|
|
* @throws PHPUnit_Framework_AssertionFailedError
|
|
|
|
* @return boolean
|
2008-05-15 09:15:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function assertExactHTMLMatchBySelector($selector, $expectedMatches) {
|
2008-05-15 09:15:33 +02:00
|
|
|
$items = $this->cssParser()->getBySelector($selector);
|
2008-08-13 05:40:56 +02:00
|
|
|
|
|
|
|
$actuals = array();
|
|
|
|
if($items) foreach($items as $item) $actuals[] = $item->asXML();
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$expectedMatches == $actuals,
|
|
|
|
"Failed asserting the CSS selector '$selector' has an exact match to the expected elements:\n'"
|
2014-07-28 23:24:40 +02:00
|
|
|
. implode("'\n'", $expectedMatches) . "'\n\n"
|
2012-11-23 14:55:19 +01:00
|
|
|
. "Instead the following elements were found:\n'" . implode("'\n'", $actuals) . "'"
|
|
|
|
);
|
2009-05-06 08:36:16 +02:00
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
/**
|
|
|
|
* Log in as the given member
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
|
|
|
* @param Member|int|string $member The ID, fixture codename, or Member object of the member that you want to log in
|
2009-05-06 08:36:16 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function logInAs($member) {
|
2009-05-06 08:36:16 +02:00
|
|
|
if(is_object($member)) $memberID = $member->ID;
|
|
|
|
elseif(is_numeric($member)) $memberID = $member;
|
2016-06-23 01:37:22 +02:00
|
|
|
else $memberID = $this->idFromFixture('SilverStripe\\Security\\Member', $member);
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
$this->session()->inst_set('loggedInAs', $memberID);
|
|
|
|
}
|
2014-07-28 23:24:40 +02:00
|
|
|
|
2008-05-26 08:21:30 +02:00
|
|
|
/**
|
|
|
|
* Use the draft (stage) site for testing.
|
2012-09-26 23:34:00 +02:00
|
|
|
* This is helpful if you're not testing publication functionality and don't want "stage management" cluttering
|
|
|
|
* your test.
|
2010-10-19 03:32:17 +02:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param bool $enabled toggle the use of the draft site
|
2008-05-26 08:21:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function useDraftSite($enabled = true) {
|
2010-10-19 03:32:17 +02:00
|
|
|
if($enabled) {
|
|
|
|
$this->session()->inst_set('readingMode', 'Stage.Stage');
|
|
|
|
$this->session()->inst_set('unsecuredDraftSite', true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->session()->inst_set('readingMode', 'Stage.Live');
|
2014-07-28 23:24:40 +02:00
|
|
|
$this->session()->inst_set('unsecuredDraftSite', false);
|
2010-10-19 03:32:17 +02:00
|
|
|
}
|
2008-05-26 08:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a static variable from this class.
|
2014-07-28 23:24:40 +02:00
|
|
|
*
|
|
|
|
* @param string $varName
|
|
|
|
* @return mixed
|
2008-05-26 08:21:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function stat($varName) {
|
2013-03-21 19:48:54 +01:00
|
|
|
return static::$varName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-28 23:24:40 +02:00
|
|
|
* @return bool
|
2013-03-21 19:48:54 +01:00
|
|
|
*/
|
|
|
|
public static function get_disable_themes() {
|
|
|
|
return static::$disable_themes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-28 23:24:40 +02:00
|
|
|
* @return bool
|
2013-03-21 19:48:54 +01:00
|
|
|
*/
|
|
|
|
public static function get_use_draft_site() {
|
|
|
|
return static::$use_draft_site;
|
2008-05-26 08:21:30 +02:00
|
|
|
}
|
2010-04-13 05:16:57 +02:00
|
|
|
}
|