Merge pull request #1277 from totten/master-reporttestcase
[civicrm-core.git] / tests / phpunit / CiviTest / CiviTestCase.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class CiviTestCase extends PHPUnit_Framework_Testcase {
36 function __construct() {
37 parent::__construct();
38
39 require_once 'CRM/Core/Config.php';
40 $config = CRM_Core_Config::singleton();
41 }
42
43 function civiGet($path, $params, $abort = FALSE) {
44 $url = CRM_Utils_System::url($path, $params, TRUE, NULL, FALSE);
45
46 return $this->civiGetURL($url, $abort);
47 }
48
49 function civiGetURL($url, $abort = FALSE) {
50 $html = $this->_browser->get($url);
51
52 if ($this->drupalCheckAuth(TRUE)) {
53 $html .= $this->drupalCheckAuth();
54 }
55
56 $this->_content = $this->_browser->getContent();
57
58 if ($abort) {
59 echo $html;
60 exit();
61 }
62
63 return $html;
64 }
65
66 function getUrlsByLabel($label, $fuzzy = FALSE) {
67 if (!$fuzzy) {
68 return $this->_browser->_page->getUrlsByLabel($label);
69 }
70
71 $matches = array();
72 foreach ($this->_browser->_page->_links as $link) {
73 $text = $link->getText();
74 if ($text == $label ||
75 strpos($text, $label) !== FALSE
76 ) {
77 $matches[] = $this->_browser->_page->_getUrlFromLink($link);
78 }
79 }
80 return $matches;
81 }
82
83 function isCiviURL($url, $ignoreVariations = TRUE) {
84 static $config = NULL;
85 if (!$config) {
86 $config = CRM_Core_Config::singleton();
87 }
88
89 if (strpos($url, $config->userFrameworkBaseURL . 'civicrm/') === FALSE) {
90 return FALSE;
91 }
92
93 // ignore all urls with snippet, force, crmSID
94 if ($ignoreVariations &&
95 (strpos($url, 'snippet=') ||
96 strpos($url, 'force=') ||
97 strpos($url, 'crmSID=')
98 )
99 ) {
100 return FALSE;
101 }
102
103 return TRUE;
104 }
105
106 function getUrlsByToken($token, $path = NULL) {
107 $matches = array();
108 foreach ($this->_browser->_page->_links as $link) {
109 $text = $link->getText();
110 $url = $this->_browser->_page->_getUrlFromLink($link)->asString();
111 if ($this->isCiviURL($url) &&
112 (strpos($url, $token) !== FALSE)
113 ) {
114 if (!$path ||
115 strpos($url, $path) !== FALSE
116 ) {
117 $matches[$text] = $url;
118 }
119 }
120 }
121 return $matches;
122 }
123
124 function clickLink($label, $index = 0, $fuzzy = FALSE) {
125 if (!$fuzzy) {
126 return parent::clickLink($label, $index);
127 }
128
129 $url_before = str_replace('%', '%%', $this->getUrl());
130 $urls = $this->getUrlsByLabel($label, TRUE);
131 if (count($urls) < $index + 1) {
132 $url_target = 'URL NOT FOUND!';
133 }
134 else {
135 $url_target = str_replace('%', '%%', $urls[$index]->asString());
136 }
137
138 $this->_browser->_load($urls[$index], new SimpleGetEncoding());
139 $ret = $this->_failOnError($this->_browser->getContent());
140
141 $this->assertTrue($ret, ' [browser] clicked link ' . t($label) . " ($url_target) from $url_before");
142
143 return $ret;
144 }
145
146 function allPermissions() {
147 return array(
148 1 => 'add contacts',
149 2 => 'view all contacts',
150 3 => 'edit all contacts',
151 4 => 'import contacts',
152 5 => 'edit groups',
153 6 => 'administer CiviCRM',
154 7 => 'access uploaded files',
155 8 => 'profile listings and forms',
156 9 => 'access all custom data',
157 10 => 'view all activities',
158 11 => 'access CiviCRM',
159 12 => 'access Contact Dashboard',
160 );
161 }
162
163 function errorPage(&$ret, &$url) {
164 // check if there is a civicrm error or warning message on the page
165 // at a later stage, we should also check for CMS based errors
166 $this->assertTrue($ret, ts(' [browser] GET %1"', array('%1' => $url)));
167
168 $this->assertNoText('Sorry. A non-recoverable error has occurred', '[browser] fatal error page?');
169 $this->assertNoText('The requested page could not be found', '[browser] page not found?');
170 $this->assertNoText('You are not authorized to access this page', '[browser] permission denied?');
171
172 return;
173 }
174}
175