2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Code quality unit tests that are fast enough to run each time.
22 * @copyright © 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 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() {
37 if ($CFG->ostype
=== 'UNIX') {
38 // try it the faster way
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);
49 // return code 0 means found, return code 1 means NOT found, 127 is grep not found
51 // executed only if no file failed the test
52 $this->assertTrue(true);
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.");