MDL-36901: Remove system paths from exceptions
[moodle.git] / lib / tests / setuplib_test.php
CommitLineData
32c842e2
TH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for setuplib.php
19 *
20 * @package core_phpunit
21 * @copyright 2012 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27
28/**
29 * Unit tests for setuplib.php
30 *
31 * @copyright 2012 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class core_setuplib_testcase extends basic_testcase {
35
36 /**
37 * Test get_docs_url_standard in the normal case when we should link to Moodle docs.
38 */
39 public function test_get_docs_url_standard() {
40 global $CFG;
41 if (empty($CFG->docroot)) {
42 $docroot = 'http://docs.moodle.org/';
43 } else {
44 $docroot = $CFG->docroot;
45 }
46 $this->assertRegExp('~^' . preg_quote($docroot, '') . '/2\d/' . current_language() . '/course/editing$~',
47 get_docs_url('course/editing'));
48 }
49
50 /**
51 * Test get_docs_url_standard in the special case of an absolute HTTP URL.
52 */
53 public function test_get_docs_url_http() {
54 $url = 'http://moodle.org/';
55 $this->assertEquals($url, get_docs_url($url));
56 }
57
58 /**
59 * Test get_docs_url_standard in the special case of an absolute HTTPS URL.
60 */
61 public function test_get_docs_url_https() {
62 $url = 'https://moodle.org/';
63 $this->assertEquals($url, get_docs_url($url));
64 }
65
66 /**
67 * Test get_docs_url_standard in the special case of a link relative to wwwroot.
68 */
69 public function test_get_docs_url_wwwroot() {
70 global $CFG;
71 $this->assertEquals($CFG->wwwroot . '/lib/tests/setuplib_test.php',
72 get_docs_url('%%WWWROOT%%/lib/tests/setuplib_test.php'));
73 }
f28af1dd
PS
74
75 public function test_is_web_crawler() {
76 $browsers = array(
77 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))',
78 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:18.0) Gecko/18.0 Firefox/18.0',
79 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412',
80 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10',
81 'Opera/9.0 (Windows NT 5.1; U; en)',
82 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 –Nexus',
83 'Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5',
84 );
85 $crawlers = array(
86 // Google
87 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
88 'Googlebot/2.1 (+http://www.googlebot.com/bot.html)',
89 'Googlebot-Image/1.0',
90 // Yahoo
91 'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)',
92 // Bing
93 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
94 'Mozilla/5.0 (compatible; bingbot/2.0 +http://www.bing.com/bingbot.htm)',
95 // MSN
96 'msnbot/2.1',
97 // Yandex
98 'Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)',
99 'Mozilla/5.0 (compatible; YandexImages/3.0; +http://yandex.com/bots)',
100 // AltaVista
101 'AltaVista V2.0B crawler@evreka.com',
102 // ZoomSpider
103 'ZoomSpider - wrensoft.com [ZSEBOT]',
104 // Baidu
105 'Baiduspider+(+http://www.baidu.com/search/spider_jp.html)',
106 'Baiduspider+(+http://www.baidu.com/search/spider.htm)',
107 'BaiDuSpider',
108 // Ask.com
109 'User-Agent: Mozilla/2.0 (compatible; Ask Jeeves/Teoma)',
110 );
111
112 foreach ($browsers as $agent) {
113 $_SERVER['HTTP_USER_AGENT'] = $agent;
114 $this->assertFalse(is_web_crawler());
115 }
116 foreach ($crawlers as $agent) {
117 $_SERVER['HTTP_USER_AGENT'] = $agent;
118 $this->assertTrue(is_web_crawler(), "$agent should be considered a search engine");
119 }
120 }
8d220cb5
MN
121
122 /**
123 * Test if get_exception_info() removes file system paths
124 */
125 public function test_exception_info_removes_serverpaths() {
126 global $CFG;
127
128 // This doesn't test them all possible ones, but these are set for unit tests.
129 $cfgnames = array('dataroot', 'dirroot', 'tempdir', 'cachedir');
130
131 $fixture = '';
132 $expected = '';
133 foreach ($cfgnames as $cfgname) {
134 if (!empty($CFG->$cfgname)) {
135 $fixture .= $CFG->$cfgname.' ';
136 $expected .= "[$cfgname] ";
137 }
138 }
139 $exception = new moodle_exception('generalexceptionmessage', 'error', '', $fixture, $fixture);
140 $exceptioninfo = get_exception_info($exception);
141
142 $this->assertContains($expected, $exceptioninfo->message, 'Exception message does not contain system paths');
143 $this->assertContains($expected, $exceptioninfo->debuginfo, 'Exception debug info does not contain system paths');
144 }
32c842e2 145}