mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
da79dff7d7
- Dispatcher sets a Controller::here variable with the real URL used to access the page, so that tag generators can that use an url (linkTo and formTag for example) use the real url, not guess it from the controller and action names which often fails - Log class works more reliably and a LogError() shortcut function was added - Nstring class added, to store string-related functions (there are just four yet, including a random password generator and an string-to-array splitter - SimpleTest library (with Rephlux) included in /vendors; I've tweaked SimpleScorer::inCli() function, because it didn't work on my setup, it should work everywhere now (it checks for empty REQUEST_METHOD, which should only be empty in CLI) git-svn-id: https://svn.cakephp.org/repo/trunk/cake@248 3807eeeb-6ff5-0310-8944-8be069107fe0
277 lines
10 KiB
HTML
277 lines
10 KiB
HTML
<html>
|
|
<head>
|
|
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
<title>Simple Test documentation for testing HTML forms</title>
|
|
<link rel="stylesheet" type="text/css" href="docs.css" title="Styles">
|
|
</head>
|
|
<body>
|
|
<div class="menu_back">
|
|
<div class="menu">
|
|
<h2>
|
|
<a href="index.html">SimpleTest</a>
|
|
</h2>
|
|
<ul>
|
|
<li>
|
|
<a href="overview.html">Overview</a>
|
|
</li>
|
|
<li>
|
|
<a href="unit_test_documentation.html">Unit tester</a>
|
|
</li>
|
|
<li>
|
|
<a href="group_test_documentation.html">Group tests</a>
|
|
</li>
|
|
<li>
|
|
<a href="server_stubs_documentation.html">Server stubs</a>
|
|
</li>
|
|
<li>
|
|
<a href="mock_objects_documentation.html">Mock objects</a>
|
|
</li>
|
|
<li>
|
|
<a href="partial_mocks_documentation.html">Partial mocks</a>
|
|
</li>
|
|
<li>
|
|
<a href="reporter_documentation.html">Reporting</a>
|
|
</li>
|
|
<li>
|
|
<a href="expectation_documentation.html">Expectations</a>
|
|
</li>
|
|
<li>
|
|
<a href="web_tester_documentation.html">Web tester</a>
|
|
</li>
|
|
<li>
|
|
<span class="chosen">Testing forms</span>
|
|
</li>
|
|
<li>
|
|
<a href="authentication_documentation.html">Authentication</a>
|
|
</li>
|
|
<li>
|
|
<a href="browser_documentation.html">Scriptable browser</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<h1>Form testing documentation</h1>
|
|
<div class="content">
|
|
<p>
|
|
<a class="target" name="submit">
|
|
<h2>Submitting a simple form</h2>
|
|
</a>
|
|
</p>
|
|
<p>
|
|
When a page is fetched by the <span class="new_code">WebTestCase</span>
|
|
using <span class="new_code">get()</span> or
|
|
<span class="new_code">post()</span> the page content is
|
|
automatically parsed.
|
|
This results in any form controls that are inside <form> tags
|
|
being available from within the test case.
|
|
For example, if we have this snippet of HTML...
|
|
<pre>
|
|
<form>
|
|
<input type="text" name="a" value="A default" />
|
|
<input type="submit" value="Go" />
|
|
</form>
|
|
</pre>
|
|
Which looks like this...
|
|
</p>
|
|
<p>
|
|
<form class="demo">
|
|
<input type="text" name="a" value="A default">
|
|
<input type="submit" value="Go">
|
|
</form>
|
|
</p>
|
|
<p>
|
|
We can navigate to this code, via the
|
|
<a href="http://www.lastcraft.com/form_testing_documentation.php">LastCraft</a>
|
|
site, with the following test...
|
|
<pre>
|
|
class SimpleFormTests extends WebTestCase {
|
|
<strong>
|
|
function testDefaultValue() {
|
|
$this->get('http://www.lastcraft.com/form_testing_documentation.php');
|
|
$this->assertField('a', 'A default');
|
|
}</strong>
|
|
}
|
|
</pre>
|
|
Immediately after loading the page all of the HTML controls are set at
|
|
their default values just as they would appear in the web browser.
|
|
The assertion tests that a HTML widget exists in the page with the
|
|
name "a" and that it is currently set to the value
|
|
"A default"
|
|
</p>
|
|
<p>
|
|
We could submit the form straight away, but first we'll change
|
|
the value of the text field and only then submit it...
|
|
<pre>
|
|
class SimpleFormTests extends WebTestCase {
|
|
|
|
function testDefaultValue() {
|
|
$this->get('http://www.my-site.com/');
|
|
$this->assertField('a', 'A default');<strong>
|
|
$this->setField('a', 'New value');
|
|
$this->clickSubmit('Go');</strong>
|
|
}
|
|
}
|
|
</pre>
|
|
Because we didn't specify a method attribute on the form tag, and
|
|
didn't specify an action either, the test case will follow
|
|
the usual browser behaviour of submitting the form data as a <em>GET</em>
|
|
request back to the same location.
|
|
SimpleTest tries to emulate typical browser behaviour as much as possible,
|
|
rather than attempting to catch missing attributes on tags.
|
|
This is because the target of the testing framework is the PHP application
|
|
logic, not syntax or other errors in the HTML code.
|
|
For HTML errors, other tools such as
|
|
<a href="http://www.w3.org/People/Raggett/tidy/">HTMLTidy</a> should be used.
|
|
</p>
|
|
<p>
|
|
If a field is not present in any form, or if an option is unavailable,
|
|
then <span class="new_code">WebTestCase::setField()</span> will return
|
|
<span class="new_code">false</span>.
|
|
For example, suppose we wish to verify that a "Superuser"
|
|
option is not present in this form...
|
|
<pre>
|
|
<strong>Select type of user to add:</strong>
|
|
<select name="type">
|
|
<option>Subscriber</option>
|
|
<option>Author</option>
|
|
<option>Administrator</option>
|
|
</select>
|
|
</pre>
|
|
Which looks like...
|
|
</p>
|
|
<p>
|
|
<form class="demo">
|
|
<strong>Select type of user to add:</strong>
|
|
<select name="type">
|
|
<option>Subscriber</option>
|
|
<option>Author</option>
|
|
<option>Administrator</option>
|
|
</select>
|
|
</form>
|
|
</p>
|
|
<p>
|
|
The following test will confirm it...
|
|
<pre>
|
|
class SimpleFormTests extends WebTestCase {
|
|
...
|
|
function testNoSuperuserChoiceAvailable() {<strong>
|
|
$this->get('http://www.lastcraft.com/form_testing_documentation.php');
|
|
$this->assertFalse($this->setField('type', 'Superuser'));</strong>
|
|
}
|
|
}
|
|
</pre>
|
|
The selection will not be changed on a failure to set
|
|
a widget value.
|
|
</p>
|
|
<p>
|
|
Here is the full list of widgets currently supported...
|
|
<ul>
|
|
<li>Text fields, including hidden and password fields.</li>
|
|
<li>Submit buttons including the button tag, although not yet reset buttons</li>
|
|
<li>Text area. This includes text wrapping behaviour.</li>
|
|
<li>Checkboxes, including multiple checkboxes in the same form.</li>
|
|
<li>Drop down selections, including multiple selects.</li>
|
|
<li>Radio buttons.</li>
|
|
<li>Images.</li>
|
|
</ul>
|
|
</p>
|
|
<p>
|
|
Although most standard HTML widgets are catered for by <em>SimpleTest</em>'s
|
|
built in parser, it is unlikely that JavaScript will be implemented
|
|
anytime soon.
|
|
</p>
|
|
|
|
<p>
|
|
<a class="target" name="multiple">
|
|
<h2>Fields with multiple values</h2>
|
|
</a>
|
|
</p>
|
|
<p>
|
|
SimpleTest can cope with two types of multivalue controls: Multiple
|
|
selection drop downs, and multiple checkboxes with the same name
|
|
within a form.
|
|
The multivalue nature of these means that setting and testing
|
|
are slightly different.
|
|
Using checkboxes as an example...
|
|
<pre>
|
|
<form class="demo">
|
|
<strong>Create privileges allowed:</strong>
|
|
<input type="checkbox" name="crud" value="c" checked><br>
|
|
<strong>Retrieve privileges allowed:</strong>
|
|
<input type="checkbox" name="crud" value="r" checked><br>
|
|
<strong>Update privileges allowed:</strong>
|
|
<input type="checkbox" name="crud" value="u" checked><br>
|
|
<strong>Destroy privileges allowed:</strong>
|
|
<input type="checkbox" name="crud" value="d" checked><br>
|
|
<input type="submit" value="Enable Privileges">
|
|
</form>
|
|
</pre>
|
|
Which renders as...
|
|
</p>
|
|
<p>
|
|
<form class="demo">
|
|
<strong>Create privileges allowed:</strong>
|
|
<input type="checkbox" name="crud" value="c" checked>
|
|
<br>
|
|
<strong>Retrieve privileges allowed:</strong>
|
|
<input type="checkbox" name="crud" value="r" checked>
|
|
<br>
|
|
<strong>Update privileges allowed:</strong>
|
|
<input type="checkbox" name="crud" value="u" checked>
|
|
<br>
|
|
<strong>Destroy privileges allowed:</strong>
|
|
<input type="checkbox" name="crud" value="d" checked>
|
|
<br>
|
|
<input type="submit" value="Enable Privileges">
|
|
</form>
|
|
</p>
|
|
<p>
|
|
If we wish to disable all but the retrieval privileges and
|
|
submit this information we can do it like this...
|
|
<pre>
|
|
class SimpleFormTests extends WebTestCase {
|
|
...<strong>
|
|
function testDisableNastyPrivileges() {
|
|
$this->get('http://www.lastcraft.com/form_testing_documentation.php');
|
|
$this->assertField('crud', array('c', 'r', 'u', 'd'));
|
|
$this->setField('crud', array('r'));
|
|
$this->clickSubmit('Enable Privileges');
|
|
}</strong>
|
|
}
|
|
</pre>
|
|
Instead of setting the field to a single value, we give it a list
|
|
of values.
|
|
We do the same when testing expected values.
|
|
We can then write other test code to confirm the effect of this, perhaps
|
|
by logging in as that user and attempting an update.
|
|
</p>
|
|
<p>
|
|
<a class="target" name="raw">
|
|
<h2>Raw posting</h2>
|
|
</a>
|
|
</p>
|
|
<p>
|
|
If you want to test a form handler, but have not yet written
|
|
or do not have access to the form itself, you can create a
|
|
form submission by hand.
|
|
<pre>
|
|
class SimpleFormTests extends WebTestCase {
|
|
...<strong>
|
|
function testAttemptedHack() {
|
|
$this->post(
|
|
'http://www.my-site.com/add_user.php',
|
|
array('type' => 'superuser'));
|
|
$this->assertNoUnwantedPattern('/user created/i');
|
|
}</strong>
|
|
}
|
|
</pre>
|
|
By adding data to the <span class="new_code">WebTestCase::post()</span>
|
|
method, we are attempting to fetch the page as a form submission.
|
|
</p>
|
|
|
|
</div>
|
|
<div class="copyright">
|
|
Copyright<br>Marcus Baker, Jason Sweat, Perrick Penet 2004
|
|
</div>
|
|
</body>
|
|
</html>
|