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

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/TemplateAdapterTest.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/GalleryTemplateAdapter.class');
22
 
 
23
 
/**
24
 
 * Test Gallery Template Adapter functionality
25
 
 * @package GalleryCore
26
 
 * @subpackage PHPUnit
27
 
 * @author Jesse Mullan
28
 
 * @version $Revision: 15513 $
29
 
 */
30
 
class TemplateAdapterTest extends GalleryTestCase {
31
 
    function GalleryTemplateAdapterTest($methodName) {
32
 
        $this->GalleryTestCase($methodName);
33
 
    }
34
 
 
35
 
    function _setupAdapterWithMocks() {
36
 
        global $gallery;
37
 
        $this->_mockUrlGenerator =& new TemplateAdapterMockUrlGenerator();
38
 
        $gallery->setUrlGenerator($this->_mockUrlGenerator);
39
 
 
40
 
        $this->_mockSmarty = new TemplateAdapterMockSmarty();
41
 
        $this->_templateAdapter = new GalleryTemplateAdapter();
42
 
 
43
 
        $this->_templateAdapter->_theme = new TemplateAdapterMockTheme();
44
 
    }
45
 
 
46
 
    function testLinkId1() {
47
 
        $test = array('urlParams' => array());
48
 
        $result = GalleryTemplateAdapter::linkId($test);
49
 
        $this->assertEquals('gbAdminLink gbLink', $result);
50
 
    }
51
 
 
52
 
    function testLinkId2() {
53
 
        $test = array('urlParams' => array('view' => 'core.:ItemAdmin'));
54
 
        $result = GalleryTemplateAdapter::linkId($test);
55
 
        $this->assertEquals('gbAdminLink gbLink-core_ItemAdmin', $result);
56
 
    }
57
 
 
58
 
    function testLinkId3() {
59
 
        $test = array('urlParams' => array('view' => '.;-_monkey'));
60
 
        $result = GalleryTemplateAdapter::linkId($test);
61
 
        $this->assertEquals('gbAdminLink gbLink-monkey', $result);
62
 
    }
63
 
 
64
 
    function testLinkId4() {
65
 
        $test = array('urlParams' => array('controller' => 'core.Monkey',
66
 
                                           'view' => 'core.Pirate',
67
 
                                           'subView' => 'core.Ninja'));
68
 
        $result = GalleryTemplateAdapter::linkId($test);
69
 
        $this->assertEquals('gbAdminLink gbLink-core_Monkey-core_Pirate-core_Ninja', $result);
70
 
    }
71
 
 
72
 
    function testLinkId5() {
73
 
        $test = array('view' => 'core.Pirate');
74
 
        $result = GalleryTemplateAdapter::linkId($test);
75
 
        $this->assertEquals('gbAdminLink gbLink-core_Pirate', $result);
76
 
    }
77
 
 
78
 
    function testLinkId6() {
79
 
        $test = array('view' => 'core.Pirate',
80
 
                      'urlParams' => array('controller' => 'core.Monkey',
81
 
                                           'view' => 'core.Pirate',
82
 
                                           'subView' => 'core.Ninja'));
83
 
        $result = GalleryTemplateAdapter::linkId($test);
84
 
        $this->assertEquals('gbAdminLink gbLink-core_Monkey-core_Pirate-core_Ninja', $result);
85
 
    }
86
 
 
87
 
    function testSafeCssName1() {
88
 
        $string = 'core.ItemAdmin';
89
 
        $result = GalleryTemplateAdapter::_safeCssName($string);
90
 
        $this->assertEquals('core_ItemAdmin', $result);
91
 
    }
92
 
 
93
 
    function testSafeCssName2() {
94
 
        $string = 'core..ItemAdmin';
95
 
        $result = GalleryTemplateAdapter::_safeCssName($string);
96
 
        $this->assertEquals('core_ItemAdmin', $result);
97
 
    }
98
 
 
99
 
    function testSafeCssName3() {
100
 
        $string = '.;-_monkey';
101
 
        $result = GalleryTemplateAdapter::_safeCssName($string);
102
 
        $this->assertEquals('-monkey', $result);
103
 
    }
104
 
 
105
 
    function testForceFullUrlOn() {
106
 
        $this->_setupAdapterWithMocks();
107
 
        $this->_templateAdapter->url(array('arg1' => 'foo=bar',
108
 
                                    'forceFullUrl' => 'anything'), $this->_mockSmarty);
109
 
 
110
 
        $this->assertEquals(array(array('foo' => 'bar'), array('forceFullUrl' => true)),
111
 
                            $this->_mockUrlGenerator->_calls[0]);
112
 
    }
113
 
 
114
 
    function testForceFullUrlOff() {
115
 
        $this->_setupAdapterWithMocks();
116
 
        $this->_templateAdapter->url(array('arg1' => 'foo=bar'), $this->_mockSmarty);
117
 
 
118
 
        $this->assertEquals(array(array('foo' => 'bar'), array()),
119
 
                            $this->_mockUrlGenerator->_calls[0]);
120
 
    }
121
 
 
122
 
    function testUrlParamsAndArgs() {
123
 
        $this->_setupAdapterWithMocks();
124
 
        $this->_templateAdapter->url(array('params' => array('view' => 'fun.View'),
125
 
                                           'arg1' => 'foo=bar'), $this->_mockSmarty);
126
 
 
127
 
        $this->assertEquals(array(array('view' => 'fun.View', 'foo' => 'bar'), array()),
128
 
                            $this->_mockUrlGenerator->_calls[0]);
129
 
    }
130
 
 
131
 
    function testUrlOptionsAndBulkOptions() {
132
 
        $this->_setupAdapterWithMocks();
133
 
        $this->_templateAdapter->url(array('options' => array('foo' => 'foo', 'bar' => 'bar'),
134
 
                                           'foo' => 'bar'), $this->_mockSmarty);
135
 
 
136
 
        $this->assertEquals(array(array(), array('foo' => 'bar', 'bar' => 'bar')),
137
 
                            $this->_mockUrlGenerator->_calls[0]);
138
 
    }
139
 
 
140
 
    function testChangeInDescendents() {
141
 
        $test = array('module' => 'pirate', 'text' => 'ninja', 'l10Domain' => 'en_US');
142
 
        $this->_setupAdapterWithMocks();
143
 
        $result = GalleryTemplateAdapter::changeInDescendents($test, $this->_mockSmarty);
144
 
        $this->assertEquals('<input type="checkbox" id="pirate_ChangeInDescendents" ' .
145
 
            'name="g2_form[changeInDescendents][pirate]"/> <label ' .
146
 
            'for="pirate_ChangeInDescendents"> ninja</label>', $result);
147
 
    }
148
 
 
149
 
    /* Make sure viewL10domain doesn't get ignored */
150
 
    function testTextViewL10domain() {
151
 
        global $gallery;
152
 
        $gallery->_translator = new TemplateAdapterMockTranslator();
153
 
        $this->_setupAdapterWithMocks();
154
 
        $this->_mockSmarty->_tpl_vars['l10Domain'] = 'en_US';
155
 
        $this->_mockSmarty->_tpl_vars['viewL10domain'] = 'te_TE';
156
 
        $translated = $this->_templateAdapter->text(array('text' => 'Test failed'),
157
 
                                                    $this->_mockSmarty);
158
 
 
159
 
        $this->assertEquals('Test passed', $translated);
160
 
    }
161
 
 
162
 
    function testThemeUrls() {
163
 
        $this->_setupAdapterWithMocks();
164
 
        $results = $this->_templateAdapter->theme(
165
 
            array('url' => 'foo.css'), $this->_mockSmarty);
166
 
        $this->assertEquals(array('href' => 'themes/mock/foo.css'),
167
 
                            $results);
168
 
    }
169
 
 
170
 
    function testThemeUrlsWithOverride() {
171
 
        global $gallery;
172
 
        $this->_setupAdapterWithMocks();
173
 
 
174
 
        $base = dirname(dirname(dirname(__FILE__))) . '/../../';
175
 
        $mockPlatform = new UnitTestPlatform();
176
 
        $mockPlatform->setReply('file_exists', array($base . 'themes/mock/local/foo.css'), 1);
177
 
        $mockPlatform->setReply('is_readable', array($base . 'themes/mock/local/foo.css'), 1);
178
 
        $gallery->_platform =& $mockPlatform;
179
 
 
180
 
        $results = $this->_templateAdapter->theme(
181
 
            array('url' => 'foo.css'), $this->_mockSmarty);
182
 
        $this->assertEquals(array('href' => 'themes/mock/local/foo.css'),
183
 
                            $results);
184
 
    }
185
 
 
186
 
    function testThemeUrlsWithOverrideSubdir() {
187
 
        global $gallery;
188
 
        $this->_setupAdapterWithMocks();
189
 
 
190
 
        $base = dirname(dirname(dirname(__FILE__))) . '/../../';
191
 
        $mockPlatform = new UnitTestPlatform();
192
 
        $mockPlatform->setReply(
193
 
            'file_exists', array($base . 'themes/mock/templates/local/foo.css'), 1);
194
 
        $mockPlatform->setReply(
195
 
            'is_readable', array($base . 'themes/mock/templates/local/foo.css'), 1);
196
 
        $gallery->_platform =& $mockPlatform;
197
 
 
198
 
        $results = $this->_templateAdapter->theme(
199
 
            array('url' => 'templates/foo.css'), $this->_mockSmarty);
200
 
        $this->assertEquals(array('href' => 'themes/mock/templates/local/foo.css'),
201
 
                            $results);
202
 
    }
203
 
 
204
 
    function testAutoCompleteIncludesAuthToken() {
205
 
        global $gallery;
206
 
        $session =& $gallery->getSession();
207
 
        $this->_setupAdapterWithMocks();
208
 
 
209
 
        $url = 'http://example.com/main.php?foo=bar';
210
 
        $this->_mockSmarty->_tpl_vars = 1;
211
 
        $this->_templateAdapter->autoComplete(
212
 
                                array('element' => 'giFormUsername'), $url, $this->_mockSmarty);
213
 
        $expectedUrl = $url . '&g2_authToken=' . $session->getAuthToken();
214
 
        $this->assertEquals(
215
 
            array('smarty_include_tpl_file' => 'gallery:modules/core/templates/AutoComplete.tpl',
216
 
                  'smarty_include_vars' => array('element' => 'giFormUsername',
217
 
                                                 'url' => $expectedUrl,
218
 
                              'callCount' => 1)),
219
 
            $this->_mockSmarty->_includes[0]);
220
 
    }
221
 
}
222
 
 
223
 
class TemplateAdapterMockTheme {
224
 
    function getId() {
225
 
        return 'mock';
226
 
    }
227
 
}
228
 
 
229
 
class TemplateAdapterMockSmarty {
230
 
    var $_includes = array();
231
 
 
232
 
    function _smarty_include($params) {
233
 
        $this->_includes[] = $params;
234
 
    }
235
 
}
236
 
 
237
 
class TemplateAdapterMockUrlGenerator {
238
 
    function generateUrl($params=array(), $options=array()) {
239
 
        $this->_calls[] = array($params, $options);
240
 
        return $params;
241
 
    }
242
 
}
243
 
 
244
 
class TemplateAdapterMockTranslator {
245
 
    function translateDomain($domain, $params) {
246
 
        if ($domain == 'te_TE' && $params['text'] == 'Test failed') {
247
 
            return array(null, 'Test passed');
248
 
        }
249
 
        return array(null, null);
250
 
    }
251
 
}
252
 
?>