PageRenderTime 95ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/simpletest/frames.php

https://github.com/quarkness/piwik
PHP | 596 lines | 298 code | 39 blank | 259 comment | 48 complexity | a95964100ce9b263b8b9dbc0674de9da MD5 | raw file
  1. <?php
  2. /**
  3. * Base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage WebTester
  6. * @version $Id: frames.php 1672 2008-03-02 04:47:34Z edwardzyang $
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/page.php');
  12. require_once(dirname(__FILE__) . '/user_agent.php');
  13. /**#@-*/
  14. /**
  15. * A composite page. Wraps a frameset page and
  16. * adds subframes. The original page will be
  17. * mostly ignored. Implements the SimplePage
  18. * interface so as to be interchangeable.
  19. * @package SimpleTest
  20. * @subpackage WebTester
  21. */
  22. class SimpleFrameset {
  23. var $_frameset;
  24. var $_frames;
  25. var $_focus;
  26. var $_names;
  27. /**
  28. * Stashes the frameset page. Will make use of the
  29. * browser to fetch the sub frames recursively.
  30. * @param SimplePage $page Frameset page.
  31. */
  32. function SimpleFrameset(&$page) {
  33. $this->_frameset = &$page;
  34. $this->_frames = array();
  35. $this->_focus = false;
  36. $this->_names = array();
  37. }
  38. /**
  39. * Adds a parsed page to the frameset.
  40. * @param SimplePage $page Frame page.
  41. * @param string $name Name of frame in frameset.
  42. * @access public
  43. */
  44. function addFrame(&$page, $name = false) {
  45. $this->_frames[] = &$page;
  46. if ($name) {
  47. $this->_names[$name] = count($this->_frames) - 1;
  48. }
  49. }
  50. /**
  51. * Replaces existing frame with another. If the
  52. * frame is nested, then the call is passed down
  53. * one level.
  54. * @param array $path Path of frame in frameset.
  55. * @param SimplePage $page Frame source.
  56. * @access public
  57. */
  58. function setFrame($path, &$page) {
  59. $name = array_shift($path);
  60. if (isset($this->_names[$name])) {
  61. $index = $this->_names[$name];
  62. } else {
  63. $index = $name - 1;
  64. }
  65. if (count($path) == 0) {
  66. $this->_frames[$index] = &$page;
  67. return;
  68. }
  69. $this->_frames[$index]->setFrame($path, $page);
  70. }
  71. /**
  72. * Accessor for current frame focus. Will be
  73. * false if no frame has focus. Will have the nested
  74. * frame focus if any.
  75. * @return array Labels or indexes of nested frames.
  76. * @access public
  77. */
  78. function getFrameFocus() {
  79. if ($this->_focus === false) {
  80. return array();
  81. }
  82. return array_merge(
  83. array($this->_getPublicNameFromIndex($this->_focus)),
  84. $this->_frames[$this->_focus]->getFrameFocus());
  85. }
  86. /**
  87. * Turns an internal array index into the frames list
  88. * into a public name, or if none, then a one offset
  89. * index.
  90. * @param integer $subject Internal index.
  91. * @return integer/string Public name.
  92. * @access private
  93. */
  94. function _getPublicNameFromIndex($subject) {
  95. foreach ($this->_names as $name => $index) {
  96. if ($subject == $index) {
  97. return $name;
  98. }
  99. }
  100. return $subject + 1;
  101. }
  102. /**
  103. * Sets the focus by index. The integer index starts from 1.
  104. * If already focused and the target frame also has frames,
  105. * then the nested frame will be focused.
  106. * @param integer $choice Chosen frame.
  107. * @return boolean True if frame exists.
  108. * @access public
  109. */
  110. function setFrameFocusByIndex($choice) {
  111. if (is_integer($this->_focus)) {
  112. if ($this->_frames[$this->_focus]->hasFrames()) {
  113. return $this->_frames[$this->_focus]->setFrameFocusByIndex($choice);
  114. }
  115. }
  116. if (($choice < 1) || ($choice > count($this->_frames))) {
  117. return false;
  118. }
  119. $this->_focus = $choice - 1;
  120. return true;
  121. }
  122. /**
  123. * Sets the focus by name. If already focused and the
  124. * target frame also has frames, then the nested frame
  125. * will be focused.
  126. * @param string $name Chosen frame.
  127. * @return boolean True if frame exists.
  128. * @access public
  129. */
  130. function setFrameFocus($name) {
  131. if (is_integer($this->_focus)) {
  132. if ($this->_frames[$this->_focus]->hasFrames()) {
  133. return $this->_frames[$this->_focus]->setFrameFocus($name);
  134. }
  135. }
  136. if (in_array($name, array_keys($this->_names))) {
  137. $this->_focus = $this->_names[$name];
  138. return true;
  139. }
  140. return false;
  141. }
  142. /**
  143. * Clears the frame focus.
  144. * @access public
  145. */
  146. function clearFrameFocus() {
  147. $this->_focus = false;
  148. $this->_clearNestedFramesFocus();
  149. }
  150. /**
  151. * Clears the frame focus for any nested frames.
  152. * @access private
  153. */
  154. function _clearNestedFramesFocus() {
  155. for ($i = 0; $i < count($this->_frames); $i++) {
  156. $this->_frames[$i]->clearFrameFocus();
  157. }
  158. }
  159. /**
  160. * Test for the presence of a frameset.
  161. * @return boolean Always true.
  162. * @access public
  163. */
  164. function hasFrames() {
  165. return true;
  166. }
  167. /**
  168. * Accessor for frames information.
  169. * @return array/string Recursive hash of frame URL strings.
  170. * The key is either a numerical
  171. * index or the name attribute.
  172. * @access public
  173. */
  174. function getFrames() {
  175. $report = array();
  176. for ($i = 0; $i < count($this->_frames); $i++) {
  177. $report[$this->_getPublicNameFromIndex($i)] =
  178. $this->_frames[$i]->getFrames();
  179. }
  180. return $report;
  181. }
  182. /**
  183. * Accessor for raw text of either all the pages or
  184. * the frame in focus.
  185. * @return string Raw unparsed content.
  186. * @access public
  187. */
  188. function getRaw() {
  189. if (is_integer($this->_focus)) {
  190. return $this->_frames[$this->_focus]->getRaw();
  191. }
  192. $raw = '';
  193. for ($i = 0; $i < count($this->_frames); $i++) {
  194. $raw .= $this->_frames[$i]->getRaw();
  195. }
  196. return $raw;
  197. }
  198. /**
  199. * Accessor for plain text of either all the pages or
  200. * the frame in focus.
  201. * @return string Plain text content.
  202. * @access public
  203. */
  204. function getText() {
  205. if (is_integer($this->_focus)) {
  206. return $this->_frames[$this->_focus]->getText();
  207. }
  208. $raw = '';
  209. for ($i = 0; $i < count($this->_frames); $i++) {
  210. $raw .= ' ' . $this->_frames[$i]->getText();
  211. }
  212. return trim($raw);
  213. }
  214. /**
  215. * Accessor for last error.
  216. * @return string Error from last response.
  217. * @access public
  218. */
  219. function getTransportError() {
  220. if (is_integer($this->_focus)) {
  221. return $this->_frames[$this->_focus]->getTransportError();
  222. }
  223. return $this->_frameset->getTransportError();
  224. }
  225. /**
  226. * Request method used to fetch this frame.
  227. * @return string GET, POST or HEAD.
  228. * @access public
  229. */
  230. function getMethod() {
  231. if (is_integer($this->_focus)) {
  232. return $this->_frames[$this->_focus]->getMethod();
  233. }
  234. return $this->_frameset->getMethod();
  235. }
  236. /**
  237. * Original resource name.
  238. * @return SimpleUrl Current url.
  239. * @access public
  240. */
  241. function getUrl() {
  242. if (is_integer($this->_focus)) {
  243. $url = $this->_frames[$this->_focus]->getUrl();
  244. $url->setTarget($this->_getPublicNameFromIndex($this->_focus));
  245. } else {
  246. $url = $this->_frameset->getUrl();
  247. }
  248. return $url;
  249. }
  250. /**
  251. * Page base URL.
  252. * @return SimpleUrl Current url.
  253. * @access public
  254. */
  255. function getBaseUrl() {
  256. if (is_integer($this->_focus)) {
  257. $url = $this->_frames[$this->_focus]->getBaseUrl();
  258. } else {
  259. $url = $this->_frameset->getBaseUrl();
  260. }
  261. return $url;
  262. }
  263. /**
  264. * Expands expandomatic URLs into fully qualified
  265. * URLs for the frameset page.
  266. * @param SimpleUrl $url Relative URL.
  267. * @return SimpleUrl Absolute URL.
  268. * @access public
  269. */
  270. function expandUrl($url) {
  271. return $this->_frameset->expandUrl($url);
  272. }
  273. /**
  274. * Original request data.
  275. * @return mixed Sent content.
  276. * @access public
  277. */
  278. function getRequestData() {
  279. if (is_integer($this->_focus)) {
  280. return $this->_frames[$this->_focus]->getRequestData();
  281. }
  282. return $this->_frameset->getRequestData();
  283. }
  284. /**
  285. * Accessor for current MIME type.
  286. * @return string MIME type as string; e.g. 'text/html'
  287. * @access public
  288. */
  289. function getMimeType() {
  290. if (is_integer($this->_focus)) {
  291. return $this->_frames[$this->_focus]->getMimeType();
  292. }
  293. return $this->_frameset->getMimeType();
  294. }
  295. /**
  296. * Accessor for last response code.
  297. * @return integer Last HTTP response code received.
  298. * @access public
  299. */
  300. function getResponseCode() {
  301. if (is_integer($this->_focus)) {
  302. return $this->_frames[$this->_focus]->getResponseCode();
  303. }
  304. return $this->_frameset->getResponseCode();
  305. }
  306. /**
  307. * Accessor for last Authentication type. Only valid
  308. * straight after a challenge (401).
  309. * @return string Description of challenge type.
  310. * @access public
  311. */
  312. function getAuthentication() {
  313. if (is_integer($this->_focus)) {
  314. return $this->_frames[$this->_focus]->getAuthentication();
  315. }
  316. return $this->_frameset->getAuthentication();
  317. }
  318. /**
  319. * Accessor for last Authentication realm. Only valid
  320. * straight after a challenge (401).
  321. * @return string Name of security realm.
  322. * @access public
  323. */
  324. function getRealm() {
  325. if (is_integer($this->_focus)) {
  326. return $this->_frames[$this->_focus]->getRealm();
  327. }
  328. return $this->_frameset->getRealm();
  329. }
  330. /**
  331. * Accessor for outgoing header information.
  332. * @return string Header block.
  333. * @access public
  334. */
  335. function getRequest() {
  336. if (is_integer($this->_focus)) {
  337. return $this->_frames[$this->_focus]->getRequest();
  338. }
  339. return $this->_frameset->getRequest();
  340. }
  341. /**
  342. * Accessor for raw header information.
  343. * @return string Header block.
  344. * @access public
  345. */
  346. function getHeaders() {
  347. if (is_integer($this->_focus)) {
  348. return $this->_frames[$this->_focus]->getHeaders();
  349. }
  350. return $this->_frameset->getHeaders();
  351. }
  352. /**
  353. * Accessor for parsed title.
  354. * @return string Title or false if no title is present.
  355. * @access public
  356. */
  357. function getTitle() {
  358. return $this->_frameset->getTitle();
  359. }
  360. /**
  361. * Accessor for a list of all fixed links.
  362. * @return array List of urls as strings.
  363. * @access public
  364. */
  365. function getUrls() {
  366. if (is_integer($this->_focus)) {
  367. return $this->_frames[$this->_focus]->getUrls();
  368. }
  369. $urls = array();
  370. foreach ($this->_frames as $frame) {
  371. $urls = array_merge($urls, $frame->getUrls());
  372. }
  373. return array_values(array_unique($urls));
  374. }
  375. /**
  376. * Accessor for URLs by the link label. Label will match
  377. * regardess of whitespace issues and case.
  378. * @param string $label Text of link.
  379. * @return array List of links with that label.
  380. * @access public
  381. */
  382. function getUrlsByLabel($label) {
  383. if (is_integer($this->_focus)) {
  384. return $this->_tagUrlsWithFrame(
  385. $this->_frames[$this->_focus]->getUrlsByLabel($label),
  386. $this->_focus);
  387. }
  388. $urls = array();
  389. foreach ($this->_frames as $index => $frame) {
  390. $urls = array_merge(
  391. $urls,
  392. $this->_tagUrlsWithFrame(
  393. $frame->getUrlsByLabel($label),
  394. $index));
  395. }
  396. return $urls;
  397. }
  398. /**
  399. * Accessor for a URL by the id attribute. If in a frameset
  400. * then the first link found with that ID attribute is
  401. * returned only. Focus on a frame if you want one from
  402. * a specific part of the frameset.
  403. * @param string $id Id attribute of link.
  404. * @return string URL with that id.
  405. * @access public
  406. */
  407. function getUrlById($id) {
  408. foreach ($this->_frames as $index => $frame) {
  409. if ($url = $frame->getUrlById($id)) {
  410. if (! $url->gettarget()) {
  411. $url->setTarget($this->_getPublicNameFromIndex($index));
  412. }
  413. return $url;
  414. }
  415. }
  416. return false;
  417. }
  418. /**
  419. * Attaches the intended frame index to a list of URLs.
  420. * @param array $urls List of SimpleUrls.
  421. * @param string $frame Name of frame or index.
  422. * @return array List of tagged URLs.
  423. * @access private
  424. */
  425. function _tagUrlsWithFrame($urls, $frame) {
  426. $tagged = array();
  427. foreach ($urls as $url) {
  428. if (! $url->getTarget()) {
  429. $url->setTarget($this->_getPublicNameFromIndex($frame));
  430. }
  431. $tagged[] = $url;
  432. }
  433. return $tagged;
  434. }
  435. /**
  436. * Finds a held form by button label. Will only
  437. * search correctly built forms.
  438. * @param SimpleSelector $selector Button finder.
  439. * @return SimpleForm Form object containing
  440. * the button.
  441. * @access public
  442. */
  443. function &getFormBySubmit($selector) {
  444. $form = &$this->_findForm('getFormBySubmit', $selector);
  445. return $form;
  446. }
  447. /**
  448. * Finds a held form by image using a selector.
  449. * Will only search correctly built forms. The first
  450. * form found either within the focused frame, or
  451. * across frames, will be the one returned.
  452. * @param SimpleSelector $selector Image finder.
  453. * @return SimpleForm Form object containing
  454. * the image.
  455. * @access public
  456. */
  457. function &getFormByImage($selector) {
  458. $form = &$this->_findForm('getFormByImage', $selector);
  459. return $form;
  460. }
  461. /**
  462. * Finds a held form by the form ID. A way of
  463. * identifying a specific form when we have control
  464. * of the HTML code. The first form found
  465. * either within the focused frame, or across frames,
  466. * will be the one returned.
  467. * @param string $id Form label.
  468. * @return SimpleForm Form object containing the matching ID.
  469. * @access public
  470. */
  471. function &getFormById($id) {
  472. $form = &$this->_findForm('getFormById', $id);
  473. return $form;
  474. }
  475. /**
  476. * General form finder. Will search all the frames or
  477. * just the one in focus.
  478. * @param string $method Method to use to find in a page.
  479. * @param string $attribute Label, name or ID.
  480. * @return SimpleForm Form object containing the matching ID.
  481. * @access private
  482. */
  483. function &_findForm($method, $attribute) {
  484. if (is_integer($this->_focus)) {
  485. $form = &$this->_findFormInFrame(
  486. $this->_frames[$this->_focus],
  487. $this->_focus,
  488. $method,
  489. $attribute);
  490. return $form;
  491. }
  492. for ($i = 0; $i < count($this->_frames); $i++) {
  493. $form = &$this->_findFormInFrame(
  494. $this->_frames[$i],
  495. $i,
  496. $method,
  497. $attribute);
  498. if ($form) {
  499. return $form;
  500. }
  501. }
  502. $null = null;
  503. return $null;
  504. }
  505. /**
  506. * Finds a form in a page using a form finding method. Will
  507. * also tag the form with the frame name it belongs in.
  508. * @param SimplePage $page Page content of frame.
  509. * @param integer $index Internal frame representation.
  510. * @param string $method Method to use to find in a page.
  511. * @param string $attribute Label, name or ID.
  512. * @return SimpleForm Form object containing the matching ID.
  513. * @access private
  514. */
  515. function &_findFormInFrame(&$page, $index, $method, $attribute) {
  516. $form = &$this->_frames[$index]->$method($attribute);
  517. if (isset($form)) {
  518. $form->setDefaultTarget($this->_getPublicNameFromIndex($index));
  519. }
  520. return $form;
  521. }
  522. /**
  523. * Sets a field on each form in which the field is
  524. * available.
  525. * @param SimpleSelector $selector Field finder.
  526. * @param string $value Value to set field to.
  527. * @return boolean True if value is valid.
  528. * @access public
  529. */
  530. function setField($selector, $value) {
  531. if (is_integer($this->_focus)) {
  532. $this->_frames[$this->_focus]->setField($selector, $value);
  533. } else {
  534. for ($i = 0; $i < count($this->_frames); $i++) {
  535. $this->_frames[$i]->setField($selector, $value);
  536. }
  537. }
  538. }
  539. /**
  540. * Accessor for a form element value within a page.
  541. * @param SimpleSelector $selector Field finder.
  542. * @return string/boolean A string if the field is
  543. * present, false if unchecked
  544. * and null if missing.
  545. * @access public
  546. */
  547. function getField($selector) {
  548. for ($i = 0; $i < count($this->_frames); $i++) {
  549. $value = $this->_frames[$i]->getField($selector);
  550. if (isset($value)) {
  551. return $value;
  552. }
  553. }
  554. return null;
  555. }
  556. }
  557. ?>