weekly release 2.7dev
[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
fba8cef3 29class core_code_testcase extends advanced_testcase {
a3d5830a
PS
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;
b136a25b
PS
36
37 if ($CFG->ostype === 'UNIX') {
96a58e7e 38 // Try it the faster way.
b136a25b
PS
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.'"';
45 }
46 $exclude = implode(' ', $exclude);
47 exec('grep -r '.$exclude.' DONOT'.'COMMIT .', $output, $code);
48 chdir($oldcwd);
96a58e7e 49 // Return code 0 means found, return code 1 means NOT found, 127 is grep not found.
b136a25b 50 if ($code == 1) {
96a58e7e 51 // Executed only if no file failed the test.
b136a25b
PS
52 $this->assertTrue(true);
53 return;
54 }
55 }
56
a3d5830a
PS
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;
63 }
64 $this->recurseFolders($CFG->dirroot, 'search_file_for_dnc', $regexp, true);
96a58e7e 65 $this->assertTrue(true); // Executed only if no file failed the test.
a3d5830a
PS
66 }
67
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.");
73 }
74 }
75 }
76}