PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/simpletest/frames.php

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