MDL-20636 fix most of the remaining codechecker issues in mod/quiz and lib/questionli...
[moodle.git] / lib / simpletestlib / page.php
bloba134eaddd1788d985ad8f1874df25b2433f01fd0
1 <?php
2 /**
3 * Base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage WebTester
6 * @version $Id$
7 */
9 /**#@+
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__) . '/http.php');
13 require_once(dirname(__FILE__) . '/parser.php');
14 require_once(dirname(__FILE__) . '/tag.php');
15 require_once(dirname(__FILE__) . '/form.php');
16 require_once(dirname(__FILE__) . '/selector.php');
17 /**#@-*/
19 /**
20 * Creates tags and widgets given HTML tag
21 * attributes.
22 * @package SimpleTest
23 * @subpackage WebTester
25 class SimpleTagBuilder {
27 /**
28 * Factory for the tag objects. Creates the
29 * appropriate tag object for the incoming tag name
30 * and attributes.
31 * @param string $name HTML tag name.
32 * @param hash $attributes Element attributes.
33 * @return SimpleTag Tag object.
34 * @access public
36 function createTag($name, $attributes) {
37 static $map = array(
38 'a' => 'SimpleAnchorTag',
39 'title' => 'SimpleTitleTag',
40 'base' => 'SimpleBaseTag',
41 'button' => 'SimpleButtonTag',
42 'textarea' => 'SimpleTextAreaTag',
43 'option' => 'SimpleOptionTag',
44 'label' => 'SimpleLabelTag',
45 'form' => 'SimpleFormTag',
46 'frame' => 'SimpleFrameTag');
47 $attributes = $this->_keysToLowerCase($attributes);
48 if (array_key_exists($name, $map)) {
49 $tag_class = $map[$name];
50 return new $tag_class($attributes);
51 } elseif ($name == 'select') {
52 return $this->_createSelectionTag($attributes);
53 } elseif ($name == 'input') {
54 return $this->_createInputTag($attributes);
56 return new SimpleTag($name, $attributes);
59 /**
60 * Factory for selection fields.
61 * @param hash $attributes Element attributes.
62 * @return SimpleTag Tag object.
63 * @access protected
65 function _createSelectionTag($attributes) {
66 if (isset($attributes['multiple'])) {
67 return new MultipleSelectionTag($attributes);
69 return new SimpleSelectionTag($attributes);
72 /**
73 * Factory for input tags.
74 * @param hash $attributes Element attributes.
75 * @return SimpleTag Tag object.
76 * @access protected
78 function _createInputTag($attributes) {
79 if (! isset($attributes['type'])) {
80 return new SimpleTextTag($attributes);
82 $type = strtolower(trim($attributes['type']));
83 $map = array(
84 'submit' => 'SimpleSubmitTag',
85 'image' => 'SimpleImageSubmitTag',
86 'checkbox' => 'SimpleCheckboxTag',
87 'radio' => 'SimpleRadioButtonTag',
88 'text' => 'SimpleTextTag',
89 'hidden' => 'SimpleTextTag',
90 'password' => 'SimpleTextTag',
91 'file' => 'SimpleUploadTag');
92 if (array_key_exists($type, $map)) {
93 $tag_class = $map[$type];
94 return new $tag_class($attributes);
96 return false;
99 /**
100 * Make the keys lower case for case insensitive look-ups.
101 * @param hash $map Hash to convert.
102 * @return hash Unchanged values, but keys lower case.
103 * @access private
105 function _keysToLowerCase($map) {
106 $lower = array();
107 foreach ($map as $key => $value) {
108 $lower[strtolower($key)] = $value;
110 return $lower;
115 * SAX event handler. Maintains a list of
116 * open tags and dispatches them as they close.
117 * @package SimpleTest
118 * @subpackage WebTester
120 class SimplePageBuilder extends SimpleSaxListener {
121 var $_tags;
122 var $_page;
123 var $_private_content_tag;
126 * Sets the builder up empty.
127 * @access public
129 function SimplePageBuilder() {
130 $this->SimpleSaxListener();
134 * Frees up any references so as to allow the PHP garbage
135 * collection from unset() to work.
136 * @access public
138 function free() {
139 unset($this->_tags);
140 unset($this->_page);
141 unset($this->_private_content_tags);
145 * Reads the raw content and send events
146 * into the page to be built.
147 * @param $response SimpleHttpResponse Fetched response.
148 * @return SimplePage Newly parsed page.
149 * @access public
151 function &parse($response) {
152 $this->_tags = array();
153 $this->_page = &$this->_createPage($response);
154 $parser = &$this->_createParser($this);
155 $parser->parse($response->getContent());
156 $this->_page->acceptPageEnd();
157 return $this->_page;
161 * Creates an empty page.
162 * @return SimplePage New unparsed page.
163 * @access protected
165 function &_createPage($response) {
166 $page = new SimplePage($response);
167 return $page;
171 * Creates the parser used with the builder.
172 * @param $listener SimpleSaxListener Target of parser.
173 * @return SimpleSaxParser Parser to generate
174 * events for the builder.
175 * @access protected
177 function &_createParser(&$listener) {
178 $parser = new SimpleHtmlSaxParser($listener);
179 return $parser;
183 * Start of element event. Opens a new tag.
184 * @param string $name Element name.
185 * @param hash $attributes Attributes without content
186 * are marked as true.
187 * @return boolean False on parse error.
188 * @access public
190 function startElement($name, $attributes) {
191 $factory = new SimpleTagBuilder();
192 $tag = $factory->createTag($name, $attributes);
193 if (! $tag) {
194 return true;
196 if ($tag->getTagName() == 'label') {
197 $this->_page->acceptLabelStart($tag);
198 $this->_openTag($tag);
199 return true;
201 if ($tag->getTagName() == 'form') {
202 $this->_page->acceptFormStart($tag);
203 return true;
205 if ($tag->getTagName() == 'frameset') {
206 $this->_page->acceptFramesetStart($tag);
207 return true;
209 if ($tag->getTagName() == 'frame') {
210 $this->_page->acceptFrame($tag);
211 return true;
213 if ($tag->isPrivateContent() && ! isset($this->_private_content_tag)) {
214 $this->_private_content_tag = &$tag;
216 if ($tag->expectEndTag()) {
217 $this->_openTag($tag);
218 return true;
220 $this->_page->acceptTag($tag);
221 return true;
225 * End of element event.
226 * @param string $name Element name.
227 * @return boolean False on parse error.
228 * @access public
230 function endElement($name) {
231 if ($name == 'label') {
232 $this->_page->acceptLabelEnd();
233 return true;
235 if ($name == 'form') {
236 $this->_page->acceptFormEnd();
237 return true;
239 if ($name == 'frameset') {
240 $this->_page->acceptFramesetEnd();
241 return true;
243 if ($this->_hasNamedTagOnOpenTagStack($name)) {
244 $tag = array_pop($this->_tags[$name]);
245 if ($tag->isPrivateContent() && $this->_private_content_tag->getTagName() == $name) {
246 unset($this->_private_content_tag);
248 $this->_addContentTagToOpenTags($tag);
249 $this->_page->acceptTag($tag);
250 return true;
252 return true;
256 * Test to see if there are any open tags awaiting
257 * closure that match the tag name.
258 * @param string $name Element name.
259 * @return boolean True if any are still open.
260 * @access private
262 function _hasNamedTagOnOpenTagStack($name) {
263 return isset($this->_tags[$name]) && (count($this->_tags[$name]) > 0);
267 * Unparsed, but relevant data. The data is added
268 * to every open tag.
269 * @param string $text May include unparsed tags.
270 * @return boolean False on parse error.
271 * @access public
273 function addContent($text) {
274 if (isset($this->_private_content_tag)) {
275 $this->_private_content_tag->addContent($text);
276 } else {
277 $this->_addContentToAllOpenTags($text);
279 return true;
283 * Any content fills all currently open tags unless it
284 * is part of an option tag.
285 * @param string $text May include unparsed tags.
286 * @access private
288 function _addContentToAllOpenTags($text) {
289 foreach (array_keys($this->_tags) as $name) {
290 for ($i = 0, $count = count($this->_tags[$name]); $i < $count; $i++) {
291 $this->_tags[$name][$i]->addContent($text);
297 * Parsed data in tag form. The parsed tag is added
298 * to every open tag. Used for adding options to select
299 * fields only.
300 * @param SimpleTag $tag Option tags only.
301 * @access private
303 function _addContentTagToOpenTags(&$tag) {
304 if ($tag->getTagName() != 'option') {
305 return;
307 foreach (array_keys($this->_tags) as $name) {
308 for ($i = 0, $count = count($this->_tags[$name]); $i < $count; $i++) {
309 $this->_tags[$name][$i]->addTag($tag);
315 * Opens a tag for receiving content. Multiple tags
316 * will be receiving input at the same time.
317 * @param SimpleTag $tag New content tag.
318 * @access private
320 function _openTag(&$tag) {
321 $name = $tag->getTagName();
322 if (! in_array($name, array_keys($this->_tags))) {
323 $this->_tags[$name] = array();
325 $this->_tags[$name][] = &$tag;
330 * A wrapper for a web page.
331 * @package SimpleTest
332 * @subpackage WebTester
334 class SimplePage {
335 var $_links;
336 var $_title;
337 var $_last_widget;
338 var $_label;
339 var $_left_over_labels;
340 var $_open_forms;
341 var $_complete_forms;
342 var $_frameset;
343 var $_frames;
344 var $_frameset_nesting_level;
345 var $_transport_error;
346 var $_raw;
347 var $_text;
348 var $_sent;
349 var $_headers;
350 var $_method;
351 var $_url;
352 var $_base = false;
353 var $_request_data;
356 * Parses a page ready to access it's contents.
357 * @param SimpleHttpResponse $response Result of HTTP fetch.
358 * @access public
360 function SimplePage($response = false) {
361 $this->_links = array();
362 $this->_title = false;
363 $this->_left_over_labels = array();
364 $this->_open_forms = array();
365 $this->_complete_forms = array();
366 $this->_frameset = false;
367 $this->_frames = array();
368 $this->_frameset_nesting_level = 0;
369 $this->_text = false;
370 if ($response) {
371 $this->_extractResponse($response);
372 } else {
373 $this->_noResponse();
378 * Extracts all of the response information.
379 * @param SimpleHttpResponse $response Response being parsed.
380 * @access private
382 function _extractResponse($response) {
383 $this->_transport_error = $response->getError();
384 $this->_raw = $response->getContent();
385 $this->_sent = $response->getSent();
386 $this->_headers = $response->getHeaders();
387 $this->_method = $response->getMethod();
388 $this->_url = $response->getUrl();
389 $this->_request_data = $response->getRequestData();
393 * Sets up a missing response.
394 * @access private
396 function _noResponse() {
397 $this->_transport_error = 'No page fetched yet';
398 $this->_raw = false;
399 $this->_sent = false;
400 $this->_headers = false;
401 $this->_method = 'GET';
402 $this->_url = false;
403 $this->_request_data = false;
407 * Original request as bytes sent down the wire.
408 * @return mixed Sent content.
409 * @access public
411 function getRequest() {
412 return $this->_sent;
416 * Accessor for raw text of page.
417 * @return string Raw unparsed content.
418 * @access public
420 function getRaw() {
421 return $this->_raw;
425 * Accessor for plain text of page as a text browser
426 * would see it.
427 * @return string Plain text of page.
428 * @access public
430 function getText() {
431 if (! $this->_text) {
432 $this->_text = SimpleHtmlSaxParser::normalise($this->_raw);
434 return $this->_text;
438 * Accessor for raw headers of page.
439 * @return string Header block as text.
440 * @access public
442 function getHeaders() {
443 if ($this->_headers) {
444 return $this->_headers->getRaw();
446 return false;
450 * Original request method.
451 * @return string GET, POST or HEAD.
452 * @access public
454 function getMethod() {
455 return $this->_method;
459 * Original resource name.
460 * @return SimpleUrl Current url.
461 * @access public
463 function getUrl() {
464 return $this->_url;
468 * Base URL if set via BASE tag page url otherwise
469 * @return SimpleUrl Base url.
470 * @access public
472 function getBaseUrl() {
473 return $this->_base;
477 * Original request data.
478 * @return mixed Sent content.
479 * @access public
481 function getRequestData() {
482 return $this->_request_data;
486 * Accessor for last error.
487 * @return string Error from last response.
488 * @access public
490 function getTransportError() {
491 return $this->_transport_error;
495 * Accessor for current MIME type.
496 * @return string MIME type as string; e.g. 'text/html'
497 * @access public
499 function getMimeType() {
500 if ($this->_headers) {
501 return $this->_headers->getMimeType();
503 return false;
507 * Accessor for HTTP response code.
508 * @return integer HTTP response code received.
509 * @access public
511 function getResponseCode() {
512 if ($this->_headers) {
513 return $this->_headers->getResponseCode();
515 return false;
519 * Accessor for last Authentication type. Only valid
520 * straight after a challenge (401).
521 * @return string Description of challenge type.
522 * @access public
524 function getAuthentication() {
525 if ($this->_headers) {
526 return $this->_headers->getAuthentication();
528 return false;
532 * Accessor for last Authentication realm. Only valid
533 * straight after a challenge (401).
534 * @return string Name of security realm.
535 * @access public
537 function getRealm() {
538 if ($this->_headers) {
539 return $this->_headers->getRealm();
541 return false;
545 * Accessor for current frame focus. Will be
546 * false as no frames.
547 * @return array Always empty.
548 * @access public
550 function getFrameFocus() {
551 return array();
555 * Sets the focus by index. The integer index starts from 1.
556 * @param integer $choice Chosen frame.
557 * @return boolean Always false.
558 * @access public
560 function setFrameFocusByIndex($choice) {
561 return false;
565 * Sets the focus by name. Always fails for a leaf page.
566 * @param string $name Chosen frame.
567 * @return boolean False as no frames.
568 * @access public
570 function setFrameFocus($name) {
571 return false;
575 * Clears the frame focus. Does nothing for a leaf page.
576 * @access public
578 function clearFrameFocus() {
582 * Adds a tag to the page.
583 * @param SimpleTag $tag Tag to accept.
584 * @access public
586 function acceptTag(&$tag) {
587 if ($tag->getTagName() == "a") {
588 $this->_addLink($tag);
589 } elseif ($tag->getTagName() == "base") {
590 $this->_setBase($tag);
591 } elseif ($tag->getTagName() == "title") {
592 $this->_setTitle($tag);
593 } elseif ($this->_isFormElement($tag->getTagName())) {
594 for ($i = 0; $i < count($this->_open_forms); $i++) {
595 $this->_open_forms[$i]->addWidget($tag);
597 $this->_last_widget = &$tag;
602 * Opens a label for a described widget.
603 * @param SimpleFormTag $tag Tag to accept.
604 * @access public
606 function acceptLabelStart(&$tag) {
607 $this->_label = &$tag;
608 unset($this->_last_widget);
612 * Closes the most recently opened label.
613 * @access public
615 function acceptLabelEnd() {
616 if (isset($this->_label)) {
617 if (isset($this->_last_widget)) {
618 $this->_last_widget->setLabel($this->_label->getText());
619 unset($this->_last_widget);
620 } else {
621 $this->_left_over_labels[] = SimpleTestCompatibility::copy($this->_label);
623 unset($this->_label);
628 * Tests to see if a tag is a possible form
629 * element.
630 * @param string $name HTML element name.
631 * @return boolean True if form element.
632 * @access private
634 function _isFormElement($name) {
635 return in_array($name, array('input', 'button', 'textarea', 'select'));
639 * Opens a form. New widgets go here.
640 * @param SimpleFormTag $tag Tag to accept.
641 * @access public
643 function acceptFormStart(&$tag) {
644 $this->_open_forms[] = new SimpleForm($tag, $this);
648 * Closes the most recently opened form.
649 * @access public
651 function acceptFormEnd() {
652 if (count($this->_open_forms)) {
653 $this->_complete_forms[] = array_pop($this->_open_forms);
658 * Opens a frameset. A frameset may contain nested
659 * frameset tags.
660 * @param SimpleFramesetTag $tag Tag to accept.
661 * @access public
663 function acceptFramesetStart(&$tag) {
664 if (! $this->_isLoadingFrames()) {
665 $this->_frameset = &$tag;
667 $this->_frameset_nesting_level++;
671 * Closes the most recently opened frameset.
672 * @access public
674 function acceptFramesetEnd() {
675 if ($this->_isLoadingFrames()) {
676 $this->_frameset_nesting_level--;
681 * Takes a single frame tag and stashes it in
682 * the current frame set.
683 * @param SimpleFrameTag $tag Tag to accept.
684 * @access public
686 function acceptFrame(&$tag) {
687 if ($this->_isLoadingFrames()) {
688 if ($tag->getAttribute('src')) {
689 $this->_frames[] = &$tag;
695 * Test to see if in the middle of reading
696 * a frameset.
697 * @return boolean True if inframeset.
698 * @access private
700 function _isLoadingFrames() {
701 if (! $this->_frameset) {
702 return false;
704 return ($this->_frameset_nesting_level > 0);
708 * Test to see if link is an absolute one.
709 * @param string $url Url to test.
710 * @return boolean True if absolute.
711 * @access protected
713 function _linkIsAbsolute($url) {
714 $parsed = new SimpleUrl($url);
715 return (boolean)($parsed->getScheme() && $parsed->getHost());
719 * Adds a link to the page.
720 * @param SimpleAnchorTag $tag Link to accept.
721 * @access protected
723 function _addLink($tag) {
724 $this->_links[] = $tag;
728 * Marker for end of complete page. Any work in
729 * progress can now be closed.
730 * @access public
732 function acceptPageEnd() {
733 while (count($this->_open_forms)) {
734 $this->_complete_forms[] = array_pop($this->_open_forms);
736 foreach ($this->_left_over_labels as $label) {
737 for ($i = 0, $count = count($this->_complete_forms); $i < $count; $i++) {
738 $this->_complete_forms[$i]->attachLabelBySelector(
739 new SimpleById($label->getFor()),
740 $label->getText());
746 * Test for the presence of a frameset.
747 * @return boolean True if frameset.
748 * @access public
750 function hasFrames() {
751 return (boolean)$this->_frameset;
755 * Accessor for frame name and source URL for every frame that
756 * will need to be loaded. Immediate children only.
757 * @return boolean/array False if no frameset or
758 * otherwise a hash of frame URLs.
759 * The key is either a numerical
760 * base one index or the name attribute.
761 * @access public
763 function getFrameset() {
764 if (! $this->_frameset) {
765 return false;
767 $urls = array();
768 for ($i = 0; $i < count($this->_frames); $i++) {
769 $name = $this->_frames[$i]->getAttribute('name');
770 $url = new SimpleUrl($this->_frames[$i]->getAttribute('src'));
771 $urls[$name ? $name : $i + 1] = $this->expandUrl($url);
773 return $urls;
777 * Fetches a list of loaded frames.
778 * @return array/string Just the URL for a single page.
779 * @access public
781 function getFrames() {
782 $url = $this->expandUrl($this->getUrl());
783 return $url->asString();
787 * Accessor for a list of all links.
788 * @return array List of urls with scheme of
789 * http or https and hostname.
790 * @access public
792 function getUrls() {
793 $all = array();
794 foreach ($this->_links as $link) {
795 $url = $this->_getUrlFromLink($link);
796 $all[] = $url->asString();
798 return $all;
802 * Accessor for URLs by the link label. Label will match
803 * regardess of whitespace issues and case.
804 * @param string $label Text of link.
805 * @return array List of links with that label.
806 * @access public
808 function getUrlsByLabel($label) {
809 $matches = array();
810 foreach ($this->_links as $link) {
811 if ($link->getText() == $label) {
812 $matches[] = $this->_getUrlFromLink($link);
815 return $matches;
819 * Accessor for a URL by the id attribute.
820 * @param string $id Id attribute of link.
821 * @return SimpleUrl URL with that id of false if none.
822 * @access public
824 function getUrlById($id) {
825 foreach ($this->_links as $link) {
826 if ($link->getAttribute('id') === (string)$id) {
827 return $this->_getUrlFromLink($link);
830 return false;
834 * Converts a link tag into a target URL.
835 * @param SimpleAnchor $link Parsed link.
836 * @return SimpleUrl URL with frame target if any.
837 * @access private
839 function _getUrlFromLink($link) {
840 $url = $this->expandUrl($link->getHref());
841 if ($link->getAttribute('target')) {
842 $url->setTarget($link->getAttribute('target'));
844 return $url;
848 * Expands expandomatic URLs into fully qualified
849 * URLs.
850 * @param SimpleUrl $url Relative URL.
851 * @return SimpleUrl Absolute URL.
852 * @access public
854 function expandUrl($url) {
855 if (! is_object($url)) {
856 $url = new SimpleUrl($url);
858 $location = $this->getBaseUrl() ? $this->getBaseUrl() : new SimpleUrl();
859 return $url->makeAbsolute($location->makeAbsolute($this->getUrl()));
863 * Sets the base url for the page.
864 * @param SimpleTag $tag Base URL for page.
865 * @access protected
867 function _setBase(&$tag) {
868 $url = $tag->getAttribute('href');
869 $this->_base = new SimpleUrl($url);
873 * Sets the title tag contents.
874 * @param SimpleTitleTag $tag Title of page.
875 * @access protected
877 function _setTitle(&$tag) {
878 $this->_title = &$tag;
882 * Accessor for parsed title.
883 * @return string Title or false if no title is present.
884 * @access public
886 function getTitle() {
887 if ($this->_title) {
888 return $this->_title->getText();
890 return false;
894 * Finds a held form by button label. Will only
895 * search correctly built forms.
896 * @param SimpleSelector $selector Button finder.
897 * @return SimpleForm Form object containing
898 * the button.
899 * @access public
901 function &getFormBySubmit($selector) {
902 for ($i = 0; $i < count($this->_complete_forms); $i++) {
903 if ($this->_complete_forms[$i]->hasSubmit($selector)) {
904 return $this->_complete_forms[$i];
907 $null = null;
908 return $null;
912 * Finds a held form by image using a selector.
913 * Will only search correctly built forms.
914 * @param SimpleSelector $selector Image finder.
915 * @return SimpleForm Form object containing
916 * the image.
917 * @access public
919 function &getFormByImage($selector) {
920 for ($i = 0; $i < count($this->_complete_forms); $i++) {
921 if ($this->_complete_forms[$i]->hasImage($selector)) {
922 return $this->_complete_forms[$i];
925 $null = null;
926 return $null;
930 * Finds a held form by the form ID. A way of
931 * identifying a specific form when we have control
932 * of the HTML code.
933 * @param string $id Form label.
934 * @return SimpleForm Form object containing the matching ID.
935 * @access public
937 function &getFormById($id) {
938 for ($i = 0; $i < count($this->_complete_forms); $i++) {
939 if ($this->_complete_forms[$i]->getId() == $id) {
940 return $this->_complete_forms[$i];
943 $null = null;
944 return $null;
948 * Sets a field on each form in which the field is
949 * available.
950 * @param SimpleSelector $selector Field finder.
951 * @param string $value Value to set field to.
952 * @return boolean True if value is valid.
953 * @access public
955 function setField($selector, $value, $position=false) {
956 $is_set = false;
957 for ($i = 0; $i < count($this->_complete_forms); $i++) {
958 if ($this->_complete_forms[$i]->setField($selector, $value, $position)) {
959 $is_set = true;
962 return $is_set;
966 * Accessor for a form element value within a page.
967 * @param SimpleSelector $selector Field finder.
968 * @return string/boolean A string if the field is
969 * present, false if unchecked
970 * and null if missing.
971 * @access public
973 function getField($selector) {
974 for ($i = 0; $i < count($this->_complete_forms); $i++) {
975 $value = $this->_complete_forms[$i]->getValue($selector);
976 if (isset($value)) {
977 return $value;
980 return null;