MDL-32149 PHPUnit test support - part 2
[moodle.git] / lib / tests / code_test.php
CommitLineData
a3d5830a
PS
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 * Code quality unit tests that are fast enough to run each time.
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
27defined('MOODLE_INTERNAL') || die();
28
29class code_testcase extends advanced_testcase {
30 protected $badstrings;
31 protected $extensions_to_ignore = array('exe', 'gif', 'ico', 'jpg', 'png', 'ttf', 'log');
32 protected $ignore_folders = array();
33
34 public function test_dnc() {
35 global $CFG;
36 $regexp = '/\.(' . implode('|', $this->extensions_to_ignore) . ')$/';
37 $this->badstrings = array();
38 $this->badstrings['DONOT' . 'COMMIT'] = 'DONOT' . 'COMMIT'; // If we put the literal string here, it fails the test!
39 $this->badstrings['trailing whitespace'] = "[\t ][\r\n]";
40 foreach ($this->badstrings as $description => $ignored) {
41 $this->allok[$description] = true;
42 }
43 $this->recurseFolders($CFG->dirroot, 'search_file_for_dnc', $regexp, true);
44 $this->assertTrue(true); // executed only if no file failed the test
45 }
46
47 protected function search_file_for_dnc($filepath) {
48 $content = file_get_contents($filepath);
49 foreach ($this->badstrings as $description => $badstring) {
50 if (stripos($content, $badstring) !== false) {
51 $this->fail("File $filepath contains $description.");
52 }
53 }
54 }
55}
56