Merge branch 'MDL-42592_master' of https://github.com/markn86/moodle
[moodle.git] / lib / tests / code_test.php
blobb20656749293f833454fc33b4d84161fbab99c1e
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.
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/>.
17 /**
18 * Code quality unit tests that are fast enough to run each time.
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
27 defined('MOODLE_INTERNAL') || die();
29 class core_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();
34 public function test_dnc() {
35 global $CFG;
37 if ($CFG->ostype === 'UNIX') {
38 // Try it the faster way.
39 $oldcwd = getcwd();
40 chdir($CFG->dirroot);
41 $output = null;
42 $exclude = array();
43 foreach ($this->extensions_to_ignore as $ext) {
44 $exclude[] = '--exclude="*.'.$ext.'"';
46 $exclude = implode(' ', $exclude);
47 exec('grep -r '.$exclude.' DONOT'.'COMMIT .', $output, $code);
48 chdir($oldcwd);
49 // Return code 0 means found, return code 1 means NOT found, 127 is grep not found.
50 if ($code == 1) {
51 // Executed only if no file failed the test.
52 $this->assertTrue(true);
53 return;
57 $regexp = '/\.(' . implode('|', $this->extensions_to_ignore) . ')$/';
58 $this->badstrings = array();
59 $this->badstrings['DONOT' . 'COMMIT'] = 'DONOT' . 'COMMIT'; // If we put the literal string here, it fails the test!
60 $this->badstrings['trailing whitespace'] = "[\t ][\r\n]";
61 foreach ($this->badstrings as $description => $ignored) {
62 $this->allok[$description] = true;
64 $this->recurseFolders($CFG->dirroot, 'search_file_for_dnc', $regexp, true);
65 $this->assertTrue(true); // Executed only if no file failed the test.
68 protected function search_file_for_dnc($filepath) {
69 $content = file_get_contents($filepath);
70 foreach ($this->badstrings as $description => $badstring) {
71 if (stripos($content, $badstring) !== false) {
72 $this->fail("File $filepath contains $description.");