~ubuntu-branches/ubuntu/maverick/gallery2/maverick

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/TemplateAuditTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or (at
9
 
 * your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19
 
 */
20
 
 
21
 
GalleryCoreApi::requireOnce('modules/core/classes/GalleryTemplate.class');
22
 
 
23
 
/**
24
 
 * Test Template functionality
25
 
 * @package GalleryCore
26
 
 * @subpackage PHPUnit
27
 
 * @author Bharat Mediratta <bharat@menalto.com>
28
 
 * @version $Revision: 15513 $
29
 
 */
30
 
class TemplateAuditTest extends CodeAuditTestCase {
31
 
 
32
 
    function TemplateAuditTest($methodName) {
33
 
        global $gallery;
34
 
 
35
 
        $this->CodeAuditTestCase($methodName);
36
 
 
37
 
        $platform =& $gallery->getPlatform();
38
 
        $this->_lineEnding = $platform->getLineEnding();
39
 
 
40
 
        $this->_revisionPattern = ' \* \$Revision(:.+)?\$';
41
 
        $lines[] = '\{\*';
42
 
        $lines[] = $this->_revisionPattern;
43
 
        $lines[] = ' \* If you want to customize this file, do not edit it directly since ' .
44
 
            'future upgrades';
45
 
        $lines[] = ' \* may overwrite it.  Instead, copy it into a new directory called ' .
46
 
            '"local" and edit that';
47
 
        $lines[] = ' \* version.  Gallery will look for that file first and use it if it exists.';
48
 
        $lines[] = ' \*}';
49
 
 
50
 
        $this->_revisionPlaceHolder = ' * REVISION PLACEHOLDER';
51
 
        $this->_headerPattern = join("(\r\n|\n)", $lines);
52
 
        $this->_headerLines = $lines;
53
 
        $this->_expectedHeaderLines = $lines;
54
 
        array_splice($this->_expectedHeaderLines, 1, 1, $this->_revisionPlaceHolder);
55
 
 
56
 
        $this->_exceptions = array();
57
 
        $this->_exceptions['header'] = array();
58
 
 
59
 
        $this->_exception['hiddenFormVars'] = array(
60
 
            'modules/digibug/templates/DigibugPrintPhotos.tpl',
61
 
            'modules/shutterfly/templates/PrintPhotos.tpl',
62
 
            'modules/uploadapplet/templates/ItemAddUploadApplet.tpl'
63
 
            );
64
 
 
65
 
        /* Get rid of backslashes so that we can perform string compares later */
66
 
        for ($i = 0; $i < sizeof($this->_expectedHeaderLines); $i++) {
67
 
            $this->_expectedHeaderLines[$i] = stripslashes($this->_expectedHeaderLines[$i]);
68
 
        }
69
 
 
70
 
        $this->_errorCount = 0;
71
 
    }
72
 
 
73
 
    /**
74
 
     * @see CodeAuditTestCase::shouldCheckFile
75
 
     */
76
 
    function shouldCheckFile($fileName) {
77
 
        if (preg_match('#Storage.schema\.tpl$|tools/repository/.*tpl#', $fileName)
78
 
                || !parent::shouldCheckFile($fileName)) {
79
 
            return false;
80
 
        }
81
 
        if (substr($fileName, -4) == '.tpl') {
82
 
            return true;
83
 
        }
84
 
 
85
 
        return false;
86
 
    }
87
 
 
88
 
    /**
89
 
     * @see CodeAuditTestCase::checkFile
90
 
     */
91
 
    function checkFile($fileName, $buffer) {
92
 
        $truncatedFileName = substr($fileName,
93
 
                                    strlen(dirname(dirname(dirname(dirname($fileName)))))+1);
94
 
 
95
 
        $this->_checkHiddenFormVars($truncatedFileName, $buffer);
96
 
 
97
 
        if (preg_match('/^' . $this->_headerPattern . '/', $buffer)) {
98
 
            return;
99
 
        } else {
100
 
            /* Grab the expected number of lines and compare them */
101
 
            $actual = split("(\r\n|\n)", $buffer, sizeof($this->_expectedHeaderLines)+1);
102
 
            array_splice($actual, sizeof($this->_expectedHeaderLines), 1);
103
 
 
104
 
            /* Get rid of the revision line, since we can't compare that */
105
 
            for ($i = 0; $i < sizeof($actual); $i++) {
106
 
                if (strstr($actual[$i], 'Revision')) {
107
 
                    list ($revision) = array_splice($actual, $i, 1, $this->_revisionPlaceHolder);
108
 
                    break;
109
 
                }
110
 
            }
111
 
 
112
 
            if (empty($revision)) {
113
 
                $this->assert(false, sprintf('[%s] Missing revision line', $truncatedFileName));
114
 
            } else if (!preg_match('/^' . $this->_revisionPattern . '/', $revision)) {
115
 
                $this->assert(false, sprintf('[%s] Invalid revision line: [%s]<br/>' .
116
 
                                             'Expected pattern: [%s]',
117
 
                                             $truncatedFileName,
118
 
                                             $revision, $this->_revisionPattern));
119
 
            }
120
 
 
121
 
            if (!isset($this->_exceptions['header'][$truncatedFileName])) {
122
 
                $this->assertEquals($this->_expectedHeaderLines, $actual,
123
 
                                    sprintf('[%s] Malformed template header', $truncatedFileName));
124
 
                $this->_errorCount++;
125
 
            }
126
 
 
127
 
            if ($this->_errorCount > 3) {
128
 
                $this->halt();
129
 
            }
130
 
        }
131
 
    }
132
 
    function _checkHiddenFormVars($fileName, $buffer) {
133
 
        $inHtmlForm = false;
134
 
        $hasHiddenFormVars = false;
135
 
        $formStartLine = 0;
136
 
        $lines = split("(\r\n|\n)", $buffer);
137
 
 
138
 
        if (in_array($fileName, $this->_exception['hiddenFormVars'])) {
139
 
            return;
140
 
        }
141
 
 
142
 
        for ($i = 0; $i < count($lines); $i++) {
143
 
            if (preg_match('/<form\s/', $lines[$i], $matches)) {
144
 
                $inHtmlForm = true;
145
 
                $formStartLine = $i;
146
 
            }
147
 
            if (preg_match('/hiddenFormVars/', $lines[$i], $matches)) {
148
 
                $hasHiddenFormVars = true;
149
 
            }
150
 
            if (preg_match('/<\/form>/', $lines[$i], $matches)) {
151
 
                $this->assert($hasHiddenFormVars, "$fileName: Form (line $formStartLine) is " .
152
 
                                "missing hidden form vars");
153
 
                $inHtmlForm = false;
154
 
                $hasHiddenFormVars = false;
155
 
            }
156
 
        }
157
 
    }
158
 
}
159
 
?>