. /** * Code quality unit tests that are fast enough to run each time. * * @package core * @category phpunit * @copyright © 2006 The Open University * @author T.J.Hunt@open.ac.uk * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ defined('MOODLE_INTERNAL') || die(); class code_testcase extends advanced_testcase { protected $badstrings; protected $extensions_to_ignore = array('exe', 'gif', 'ico', 'jpg', 'png', 'ttf', 'log'); protected $ignore_folders = array(); public function test_dnc() { global $CFG; if ($CFG->ostype === 'UNIX') { // try it the faster way $oldcwd = getcwd(); chdir($CFG->dirroot); $output = null; $exclude = array(); foreach ($this->extensions_to_ignore as $ext) { $exclude[] = '--exclude="*.'.$ext.'"'; } $exclude = implode(' ', $exclude); exec('grep -r '.$exclude.' DONOT'.'COMMIT .', $output, $code); chdir($oldcwd); // return code 0 means found, return code 1 means NOT found, 127 is grep not found if ($code == 1) { // executed only if no file failed the test $this->assertTrue(true); return; } } $regexp = '/\.(' . implode('|', $this->extensions_to_ignore) . ')$/'; $this->badstrings = array(); $this->badstrings['DONOT' . 'COMMIT'] = 'DONOT' . 'COMMIT'; // If we put the literal string here, it fails the test! $this->badstrings['trailing whitespace'] = "[\t ][\r\n]"; foreach ($this->badstrings as $description => $ignored) { $this->allok[$description] = true; } $this->recurseFolders($CFG->dirroot, 'search_file_for_dnc', $regexp, true); $this->assertTrue(true); // executed only if no file failed the test } protected function search_file_for_dnc($filepath) { $content = file_get_contents($filepath); foreach ($this->badstrings as $description => $badstring) { if (stripos($content, $badstring) !== false) { $this->fail("File $filepath contains $description."); } } } }