[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/tests/ -> weblib_test.php (source)

   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   * Weblib tests.
  19   *
  20   * @package    core
  21   * @category   phpunit
  22   * @copyright  &copy; 2006 The Open University
  23   * @author     T.J.Hunt@open.ac.uk
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  class core_weblib_testcase extends advanced_testcase {
  31  
  32      public function test_format_string() {
  33          global $CFG;
  34  
  35          // Ampersands.
  36          $this->assertSame("&amp; &amp;&amp;&amp;&amp;&amp; &amp;&amp;", format_string("& &&&&& &&"));
  37          $this->assertSame("ANother &amp; &amp;&amp;&amp;&amp;&amp; Category", format_string("ANother & &&&&& Category"));
  38          $this->assertSame("ANother &amp; &amp;&amp;&amp;&amp;&amp; Category", format_string("ANother & &&&&& Category", true));
  39          $this->assertSame("Nick's Test Site &amp; Other things", format_string("Nick's Test Site & Other things", true));
  40          $this->assertSame("& < > \" '", format_string("& < > \" '", true, ['escape' => false]));
  41  
  42          // String entities.
  43          $this->assertSame("&quot;", format_string("&quot;"));
  44  
  45          // Digital entities.
  46          $this->assertSame("&11234;", format_string("&11234;"));
  47  
  48          // Unicode entities.
  49          $this->assertSame("&#4475;", format_string("&#4475;"));
  50  
  51          // < and > signs.
  52          $originalformatstringstriptags = $CFG->formatstringstriptags;
  53  
  54          $CFG->formatstringstriptags = false;
  55          $this->assertSame('x &lt; 1', format_string('x < 1'));
  56          $this->assertSame('x &gt; 1', format_string('x > 1'));
  57          $this->assertSame('x &lt; 1 and x &gt; 0', format_string('x < 1 and x > 0'));
  58  
  59          $CFG->formatstringstriptags = true;
  60          $this->assertSame('x &lt; 1', format_string('x < 1'));
  61          $this->assertSame('x &gt; 1', format_string('x > 1'));
  62          $this->assertSame('x &lt; 1 and x &gt; 0', format_string('x < 1 and x > 0'));
  63  
  64          $CFG->formatstringstriptags = $originalformatstringstriptags;
  65      }
  66  
  67      public function test_s() {
  68          // Special cases.
  69          $this->assertSame('0', s(0));
  70          $this->assertSame('0', s('0'));
  71          $this->assertSame('0', s(false));
  72          $this->assertSame('', s(null));
  73  
  74          // Normal cases.
  75          $this->assertSame('This Breaks &quot; Strict', s('This Breaks " Strict'));
  76          $this->assertSame('This Breaks &lt;a&gt;&quot; Strict&lt;/a&gt;', s('This Breaks <a>" Strict</a>'));
  77  
  78          // Unicode characters.
  79          $this->assertSame('Café', s('Café'));
  80          $this->assertSame('一, 二, 三', s('一, 二, 三'));
  81  
  82          // Don't escape already-escaped numeric entities. (Note, this behaviour
  83          // may not be desirable. Perhaps we should remove these tests and that
  84          // functionality, but we can only do that if we understand why it was added.)
  85          $this->assertSame('An entity: &#x09ff;.', s('An entity: &#x09ff;.'));
  86          $this->assertSame('An entity: &#1073;.', s('An entity: &#1073;.'));
  87          $this->assertSame('An entity: &amp;amp;.', s('An entity: &amp;.'));
  88          $this->assertSame('Not an entity: &amp;amp;#x09ff;.', s('Not an entity: &amp;#x09ff;.'));
  89      }
  90  
  91      public function test_format_text_email() {
  92          $this->assertSame("This is a TEST\n",
  93              format_text_email('<p>This is a <strong>test</strong></p>', FORMAT_HTML));
  94          $this->assertSame("This is a TEST\n",
  95              format_text_email('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>', FORMAT_HTML));
  96          $this->assertSame('& so is this',
  97              format_text_email('&amp; so is this', FORMAT_HTML));
  98          $this->assertSame('Two bullets: ' . core_text::code2utf8(8226) . ' ' . core_text::code2utf8(8226),
  99              format_text_email('Two bullets: &#x2022; &#8226;', FORMAT_HTML));
 100          $this->assertSame(core_text::code2utf8(0x7fd2).core_text::code2utf8(0x7fd2),
 101              format_text_email('&#x7fd2;&#x7FD2;', FORMAT_HTML));
 102      }
 103  
 104      public function test_obfuscate_email() {
 105          $email = 'some.user@example.com';
 106          $obfuscated = obfuscate_email($email);
 107          $this->assertNotSame($email, $obfuscated);
 108          $back = core_text::entities_to_utf8(urldecode($email), true);
 109          $this->assertSame($email, $back);
 110      }
 111  
 112      public function test_obfuscate_text() {
 113          $text = 'Žluťoučký koníček 32131';
 114          $obfuscated = obfuscate_text($text);
 115          $this->assertNotSame($text, $obfuscated);
 116          $back = core_text::entities_to_utf8($obfuscated, true);
 117          $this->assertSame($text, $back);
 118      }
 119  
 120      public function test_highlight() {
 121          $this->assertSame('This is <span class="highlight">good</span>',
 122                  highlight('good', 'This is good'));
 123  
 124          $this->assertSame('<span class="highlight">span</span>',
 125                  highlight('SpaN', 'span'));
 126  
 127          $this->assertSame('<span class="highlight">SpaN</span>',
 128                  highlight('span', 'SpaN'));
 129  
 130          $this->assertSame('<span><span class="highlight">span</span></span>',
 131                  highlight('span', '<span>span</span>'));
 132  
 133          $this->assertSame('He <span class="highlight">is</span> <span class="highlight">good</span>',
 134                  highlight('good is', 'He is good'));
 135  
 136          $this->assertSame('This is <span class="highlight">good</span>',
 137                  highlight('+good', 'This is good'));
 138  
 139          $this->assertSame('This is good',
 140                  highlight('-good', 'This is good'));
 141  
 142          $this->assertSame('This is goodness',
 143                  highlight('+good', 'This is goodness'));
 144  
 145          $this->assertSame('This is <span class="highlight">good</span>ness',
 146                  highlight('good', 'This is goodness'));
 147  
 148          $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
 149                  highlight('test 1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
 150  
 151          $this->assertSame('<p><b>test</b> <b>1</b></p><p><b>1</b></p>',
 152                      highlight('test +1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
 153  
 154          $this->assertSame('<p><b>test</b> 1</p><p>1</p>',
 155                      highlight('test -1', '<p>test 1</p><p>1</p>', false, '<b>', '</b>'));
 156      }
 157  
 158      public function test_replace_ampersands() {
 159          $this->assertSame("This &amp; that &nbsp;", replace_ampersands_not_followed_by_entity("This & that &nbsp;"));
 160          $this->assertSame("This &amp;nbsp that &nbsp;", replace_ampersands_not_followed_by_entity("This &nbsp that &nbsp;"));
 161      }
 162  
 163      public function test_strip_links() {
 164          $this->assertSame('this is a link', strip_links('this is a <a href="http://someaddress.com/query">link</a>'));
 165      }
 166  
 167      public function test_wikify_links() {
 168          $this->assertSame('this is a link [ http://someaddress.com/query ]', wikify_links('this is a <a href="http://someaddress.com/query">link</a>'));
 169      }
 170  
 171      /**
 172       * Test basic moodle_url construction.
 173       */
 174      public function test_moodle_url_constructor() {
 175          global $CFG;
 176  
 177          $url = new moodle_url('/index.php');
 178          $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
 179  
 180          $url = new moodle_url('/index.php', array());
 181          $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
 182  
 183          $url = new moodle_url('/index.php', array('id' => 2));
 184          $this->assertSame($CFG->wwwroot.'/index.php?id=2', $url->out());
 185  
 186          $url = new moodle_url('/index.php', array('id' => 'two'));
 187          $this->assertSame($CFG->wwwroot.'/index.php?id=two', $url->out());
 188  
 189          $url = new moodle_url('/index.php', array('id' => 1, 'cid' => '2'));
 190          $this->assertSame($CFG->wwwroot.'/index.php?id=1&amp;cid=2', $url->out());
 191          $this->assertSame($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out(false));
 192  
 193          $url = new moodle_url('/index.php', null, 'test');
 194          $this->assertSame($CFG->wwwroot.'/index.php#test', $url->out());
 195  
 196          $url = new moodle_url('/index.php', array('id' => 2), 'test');
 197          $this->assertSame($CFG->wwwroot.'/index.php?id=2#test', $url->out());
 198      }
 199  
 200      /**
 201       * Tests moodle_url::get_path().
 202       */
 203      public function test_moodle_url_get_path() {
 204          $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
 205          $this->assertSame('/my/file/is/here.txt', $url->get_path());
 206  
 207          $url = new moodle_url('http://www.example.org/');
 208          $this->assertSame('/', $url->get_path());
 209  
 210          $url = new moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
 211          $this->assertSame('/pluginfile.php/slash/arguments', $url->get_path());
 212          $this->assertSame('/pluginfile.php', $url->get_path(false));
 213      }
 214  
 215      public function test_moodle_url_round_trip() {
 216          $strurl = 'http://moodle.org/course/view.php?id=5';
 217          $url = new moodle_url($strurl);
 218          $this->assertSame($strurl, $url->out(false));
 219  
 220          $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
 221          $url = new moodle_url($strurl);
 222          $this->assertSame($strurl, $url->out(false));
 223      }
 224  
 225      /**
 226       * Test Moodle URL objects created with a param with empty value.
 227       */
 228      public function test_moodle_url_empty_param_values() {
 229          $strurl = 'http://moodle.org/course/view.php?id=0';
 230          $url = new moodle_url($strurl, array('id' => 0));
 231          $this->assertSame($strurl, $url->out(false));
 232  
 233          $strurl = 'http://moodle.org/course/view.php?id';
 234          $url = new moodle_url($strurl, array('id' => false));
 235          $this->assertSame($strurl, $url->out(false));
 236  
 237          $strurl = 'http://moodle.org/course/view.php?id';
 238          $url = new moodle_url($strurl, array('id' => null));
 239          $this->assertSame($strurl, $url->out(false));
 240  
 241          $strurl = 'http://moodle.org/course/view.php?id';
 242          $url = new moodle_url($strurl, array('id' => ''));
 243          $this->assertSame($strurl, $url->out(false));
 244  
 245          $strurl = 'http://moodle.org/course/view.php?id';
 246          $url = new moodle_url($strurl);
 247          $this->assertSame($strurl, $url->out(false));
 248      }
 249  
 250      /**
 251       * Test set good scheme on Moodle URL objects.
 252       */
 253      public function test_moodle_url_set_good_scheme() {
 254          $url = new moodle_url('http://moodle.org/foo/bar');
 255          $url->set_scheme('myscheme');
 256          $this->assertSame('myscheme://moodle.org/foo/bar', $url->out());
 257      }
 258  
 259      /**
 260       * Test set bad scheme on Moodle URL objects.
 261       *
 262       * @expectedException coding_exception
 263       */
 264      public function test_moodle_url_set_bad_scheme() {
 265          $url = new moodle_url('http://moodle.org/foo/bar');
 266          $url->set_scheme('not a valid $ scheme');
 267      }
 268  
 269      public function test_moodle_url_round_trip_array_params() {
 270          $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
 271          $url = new moodle_url($strurl);
 272          $this->assertSame($strurl, $url->out(false));
 273  
 274          $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2');
 275          $this->assertSame($strurl, $url->out(false));
 276  
 277          // For un-keyed array params, we expect 0..n keys to be returned.
 278          $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
 279          $url = new moodle_url('http://example.com/?a[]=0&a[]=1');
 280          $this->assertSame($strurl, $url->out(false));
 281      }
 282  
 283      public function test_compare_url() {
 284          $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2));
 285          $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
 286  
 287          $this->assertFalse($url1->compare($url2, URL_MATCH_BASE));
 288          $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
 289          $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
 290  
 291          $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
 292  
 293          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 294          $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
 295          $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
 296  
 297          $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
 298  
 299          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 300          $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
 301          $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
 302  
 303          $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
 304  
 305          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 306          $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
 307          $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
 308  
 309          $url1->set_anchor('test');
 310          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 311          $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
 312          $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
 313  
 314          $url2->set_anchor('test');
 315          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 316          $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
 317          $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
 318      }
 319  
 320      public function test_out_as_local_url() {
 321          global $CFG;
 322          // Test http url.
 323          $url1 = new moodle_url('/lib/tests/weblib_test.php');
 324          $this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url());
 325  
 326          // Test https url.
 327          $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot);
 328          $url2 = new moodle_url($httpswwwroot.'/login/profile.php');
 329          $this->assertSame('/login/profile.php', $url2->out_as_local_url());
 330  
 331          // Test http url matching wwwroot.
 332          $url3 = new moodle_url($CFG->wwwroot);
 333          $this->assertSame('', $url3->out_as_local_url());
 334  
 335          // Test http url matching wwwroot ending with slash (/).
 336          $url3 = new moodle_url($CFG->wwwroot.'/');
 337          $this->assertSame('/', $url3->out_as_local_url());
 338      }
 339  
 340      /**
 341       * @expectedException coding_exception
 342       * @return void
 343       */
 344      public function test_out_as_local_url_error() {
 345          $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php');
 346          $url2->out_as_local_url();
 347      }
 348  
 349      /**
 350       * You should get error with modified url
 351       *
 352       * @expectedException coding_exception
 353       * @return void
 354       */
 355      public function test_modified_url_out_as_local_url_error() {
 356          global $CFG;
 357  
 358          $modifiedurl = $CFG->wwwroot.'1';
 359          $url3 = new moodle_url($modifiedurl.'/login/profile.php');
 360          $url3->out_as_local_url();
 361      }
 362  
 363      /**
 364       * Try get local url from external https url and you should get error
 365       *
 366       * @expectedException coding_exception
 367       */
 368      public function test_https_out_as_local_url_error() {
 369          $url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php');
 370          $url4->out_as_local_url();
 371      }
 372  
 373      public function test_moodle_url_get_scheme() {
 374          // Should return the scheme only.
 375          $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
 376          $this->assertSame('http', $url->get_scheme());
 377  
 378          // Should work for secure URLs.
 379          $url = new moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1');
 380          $this->assertSame('https', $url->get_scheme());
 381  
 382          // Should return an empty string if no scheme is specified.
 383          $url = new moodle_url('www.example.org:447/my/file/is/here.txt?really=1');
 384          $this->assertSame('', $url->get_scheme());
 385      }
 386  
 387      public function test_moodle_url_get_host() {
 388          // Should return the host part only.
 389          $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
 390          $this->assertSame('www.example.org', $url->get_host());
 391      }
 392  
 393      public function test_moodle_url_get_port() {
 394          // Should return the port if one provided.
 395          $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
 396          $this->assertSame(447, $url->get_port());
 397  
 398          // Should return an empty string if port not specified.
 399          $url = new moodle_url('http://www.example.org/some/path/here.php');
 400          $this->assertSame('', $url->get_port());
 401      }
 402  
 403      public function test_clean_text() {
 404          $text = "lala <applet>xx</applet>";
 405          $this->assertSame($text, clean_text($text, FORMAT_PLAIN));
 406          $this->assertSame('lala xx', clean_text($text, FORMAT_MARKDOWN));
 407          $this->assertSame('lala xx', clean_text($text, FORMAT_MOODLE));
 408          $this->assertSame('lala xx', clean_text($text, FORMAT_HTML));
 409      }
 410  
 411      public function test_qualified_me() {
 412          global $PAGE, $FULLME, $CFG;
 413          $this->resetAfterTest();
 414  
 415          $PAGE = new moodle_page();
 416  
 417          $FULLME = $CFG->wwwroot.'/course/view.php?id=1&xx=yy';
 418          $this->assertSame($FULLME, qualified_me());
 419  
 420          $PAGE->set_url('/course/view.php', array('id'=>1));
 421          $this->assertSame($CFG->wwwroot.'/course/view.php?id=1', qualified_me());
 422      }
 423  
 424      public function test_null_progres_trace() {
 425          $this->resetAfterTest(false);
 426  
 427          $trace = new null_progress_trace();
 428          $trace->output('do');
 429          $trace->output('re', 1);
 430          $trace->output('mi', 2);
 431          $trace->finished();
 432          $output = ob_get_contents();
 433          $this->assertSame('', $output);
 434          $this->expectOutputString('');
 435      }
 436  
 437      public function test_text_progres_trace() {
 438          $this->resetAfterTest(false);
 439  
 440          $trace = new text_progress_trace();
 441          $trace->output('do');
 442          $trace->output('re', 1);
 443          $trace->output('mi', 2);
 444          $trace->finished();
 445          $this->expectOutputString("do\n  re\n    mi\n");
 446      }
 447  
 448      public function test_html_progres_trace() {
 449          $this->resetAfterTest(false);
 450  
 451          $trace = new html_progress_trace();
 452          $trace->output('do');
 453          $trace->output('re', 1);
 454          $trace->output('mi', 2);
 455          $trace->finished();
 456          $this->expectOutputString("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n");
 457      }
 458  
 459      public function test_html_list_progress_trace() {
 460          $this->resetAfterTest(false);
 461  
 462          $trace = new html_list_progress_trace();
 463          $trace->output('do');
 464          $trace->output('re', 1);
 465          $trace->output('mi', 2);
 466          $trace->finished();
 467          $this->expectOutputString("<ul>\n<li>do<ul>\n<li>re<ul>\n<li>mi</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n");
 468      }
 469  
 470      public function test_progres_trace_buffer() {
 471          $this->resetAfterTest(false);
 472  
 473          $trace = new progress_trace_buffer(new html_progress_trace());
 474          ob_start();
 475          $trace->output('do');
 476          $trace->output('re', 1);
 477          $trace->output('mi', 2);
 478          $trace->finished();
 479          $output = ob_get_contents();
 480          ob_end_clean();
 481          $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $output);
 482          $this->assertSame($output, $trace->get_buffer());
 483  
 484          $trace = new progress_trace_buffer(new html_progress_trace(), false);
 485          $trace->output('do');
 486          $trace->output('re', 1);
 487          $trace->output('mi', 2);
 488          $trace->finished();
 489          $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
 490          $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
 491          $trace->reset_buffer();
 492          $this->assertSame('', $trace->get_buffer());
 493          $this->expectOutputString('');
 494      }
 495  
 496      public function test_combined_progres_trace() {
 497          $this->resetAfterTest(false);
 498  
 499          $trace1 = new progress_trace_buffer(new html_progress_trace(), false);
 500          $trace2 = new progress_trace_buffer(new text_progress_trace(), false);
 501  
 502          $trace = new combined_progress_trace(array($trace1, $trace2));
 503          $trace->output('do');
 504          $trace->output('re', 1);
 505          $trace->output('mi', 2);
 506          $trace->finished();
 507          $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace1->get_buffer());
 508          $this->assertSame("do\n  re\n    mi\n", $trace2->get_buffer());
 509          $this->expectOutputString('');
 510      }
 511  
 512      public function test_set_debugging() {
 513          global $CFG;
 514  
 515          $this->resetAfterTest();
 516  
 517          $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
 518          $this->assertTrue($CFG->debugdeveloper);
 519          $this->assertNotEmpty($CFG->debugdisplay);
 520  
 521          set_debugging(DEBUG_DEVELOPER, true);
 522          $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
 523          $this->assertTrue($CFG->debugdeveloper);
 524          $this->assertNotEmpty($CFG->debugdisplay);
 525  
 526          set_debugging(DEBUG_DEVELOPER, false);
 527          $this->assertEquals(DEBUG_DEVELOPER, $CFG->debug);
 528          $this->assertTrue($CFG->debugdeveloper);
 529          $this->assertEmpty($CFG->debugdisplay);
 530  
 531          set_debugging(-1);
 532          $this->assertEquals(-1, $CFG->debug);
 533          $this->assertTrue($CFG->debugdeveloper);
 534  
 535          set_debugging(DEBUG_ALL);
 536          $this->assertEquals(DEBUG_ALL, $CFG->debug);
 537          $this->assertFalse($CFG->debugdeveloper);
 538  
 539          set_debugging(DEBUG_NORMAL);
 540          $this->assertEquals(DEBUG_NORMAL, $CFG->debug);
 541          $this->assertFalse($CFG->debugdeveloper);
 542  
 543          set_debugging(DEBUG_MINIMAL);
 544          $this->assertEquals(DEBUG_MINIMAL, $CFG->debug);
 545          $this->assertFalse($CFG->debugdeveloper);
 546  
 547          set_debugging(DEBUG_NONE);
 548          $this->assertEquals(DEBUG_NONE, $CFG->debug);
 549          $this->assertFalse($CFG->debugdeveloper);
 550      }
 551  
 552      public function test_strip_pluginfile_content() {
 553          $source = <<<SOURCE
 554  Hello!
 555  
 556  I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
 557  
 558  URL outside a tag: https://moodle.org/logo/logo-240x60.gif
 559  Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
 560  
 561  External link 1:<img src='https://moodle.org/logo/logo-240x60.gif' alt='Moodle'/>
 562  External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/>
 563  Internal link 1:<img src='@@PLUGINFILE@@/logo-240x60.gif' alt='Moodle'/>
 564  Internal link 2:<img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/>
 565  Anchor link 1:<a href="@@PLUGINFILE@@logo-240x60.gif" alt="bananas">Link text</a>
 566  Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
 567  Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"><img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/></a>
 568  Ext. anchor + img:<a href="@@PLUGINFILE@@logo-240x60.gif"><img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/></a>
 569  SOURCE;
 570          $expected = <<<EXPECTED
 571  Hello!
 572  
 573  I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
 574  
 575  URL outside a tag: https://moodle.org/logo/logo-240x60.gif
 576  Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
 577  
 578  External link 1:<img src="https://moodle.org/logo/logo-240x60.gif" alt="Moodle" />
 579  External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
 580  Internal link 1:
 581  Internal link 2:
 582  Anchor link 1:Link text
 583  Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
 584  Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"></a>
 585  Ext. anchor + img:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
 586  EXPECTED;
 587          $this->assertSame($expected, strip_pluginfile_content($source));
 588      }
 589  
 590      public function test_purify_html_ruby() {
 591  
 592          $this->resetAfterTest();
 593  
 594          $ruby =
 595              "<p><ruby><rb>京都</rb><rp>(</rp><rt>きょうと</rt><rp>)</rp></ruby>は" .
 596              "<ruby><rb>日本</rb><rp>(</rp><rt>にほん</rt><rp>)</rp></ruby>の" .
 597              "<ruby><rb>都</rb><rp>(</rp><rt>みやこ</rt><rp>)</rp></ruby>です。</p>";
 598          $illegal = '<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>';
 599  
 600          $cleaned = purify_html($ruby . $illegal);
 601          $this->assertEquals($ruby, $cleaned);
 602  
 603      }
 604  
 605      /**
 606       * Tests for content_to_text.
 607       *
 608       * @param string    $content   The content
 609       * @param int|false $format    The content format
 610       * @param string    $expected  Expected value
 611       * @dataProvider provider_content_to_text
 612       */
 613      public function test_content_to_text($content, $format, $expected) {
 614          $content = content_to_text($content, $format);
 615          $this->assertEquals($expected, $content);
 616      }
 617  
 618      /**
 619       * Data provider for test_content_to_text.
 620       */
 621      public static function provider_content_to_text() {
 622          return array(
 623              array('asd', false, 'asd'),
 624              // Trim '\r\n '.
 625              array("Note that:\n\n3 > 1 ", FORMAT_PLAIN, "Note that:\n\n3 > 1"),
 626              array("Note that:\n\n3 > 1\r\n", FORMAT_PLAIN, "Note that:\n\n3 > 1"),
 627              // Multiple spaces to one.
 628              array('<span class="eheh">京都</span>  ->  hehe', FORMAT_HTML, '京都 -> hehe'),
 629              array('<span class="eheh">京都</span>  ->  hehe', false, '京都 -> hehe'),
 630              array('asd    asd', false, 'asd asd'),
 631              // From markdown to html and html to text.
 632              array('asd __lera__ con la', FORMAT_MARKDOWN, 'asd LERA con la'),
 633              // HTML to text.
 634              array('<p class="frogs">This is a <strong class=\'fishes\'>test</strong></p>', FORMAT_HTML, 'This is a TEST'),
 635              array("<span lang='en' class='multilang'>english</span>
 636  <span lang='ca' class='multilang'>català</span>
 637  <span lang='es' class='multilang'>español</span>
 638  <span lang='fr' class='multilang'>français</span>", FORMAT_HTML, "english català español français")
 639          );
 640      }
 641  
 642  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1