3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
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.
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.
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.
21
GalleryCoreApi::requireOnce('modules/core/classes/GalleryTemplateAdapter.class');
24
* Test Gallery Template Adapter functionality
25
* @package GalleryCore
27
* @author Jesse Mullan
28
* @version $Revision: 15513 $
30
class TemplateAdapterTest extends GalleryTestCase {
31
function GalleryTemplateAdapterTest($methodName) {
32
$this->GalleryTestCase($methodName);
35
function _setupAdapterWithMocks() {
37
$this->_mockUrlGenerator =& new TemplateAdapterMockUrlGenerator();
38
$gallery->setUrlGenerator($this->_mockUrlGenerator);
40
$this->_mockSmarty = new TemplateAdapterMockSmarty();
41
$this->_templateAdapter = new GalleryTemplateAdapter();
43
$this->_templateAdapter->_theme = new TemplateAdapterMockTheme();
46
function testLinkId1() {
47
$test = array('urlParams' => array());
48
$result = GalleryTemplateAdapter::linkId($test);
49
$this->assertEquals('gbAdminLink gbLink', $result);
52
function testLinkId2() {
53
$test = array('urlParams' => array('view' => 'core.:ItemAdmin'));
54
$result = GalleryTemplateAdapter::linkId($test);
55
$this->assertEquals('gbAdminLink gbLink-core_ItemAdmin', $result);
58
function testLinkId3() {
59
$test = array('urlParams' => array('view' => '.;-_monkey'));
60
$result = GalleryTemplateAdapter::linkId($test);
61
$this->assertEquals('gbAdminLink gbLink-monkey', $result);
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);
72
function testLinkId5() {
73
$test = array('view' => 'core.Pirate');
74
$result = GalleryTemplateAdapter::linkId($test);
75
$this->assertEquals('gbAdminLink gbLink-core_Pirate', $result);
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);
87
function testSafeCssName1() {
88
$string = 'core.ItemAdmin';
89
$result = GalleryTemplateAdapter::_safeCssName($string);
90
$this->assertEquals('core_ItemAdmin', $result);
93
function testSafeCssName2() {
94
$string = 'core..ItemAdmin';
95
$result = GalleryTemplateAdapter::_safeCssName($string);
96
$this->assertEquals('core_ItemAdmin', $result);
99
function testSafeCssName3() {
100
$string = '.;-_monkey';
101
$result = GalleryTemplateAdapter::_safeCssName($string);
102
$this->assertEquals('-monkey', $result);
105
function testForceFullUrlOn() {
106
$this->_setupAdapterWithMocks();
107
$this->_templateAdapter->url(array('arg1' => 'foo=bar',
108
'forceFullUrl' => 'anything'), $this->_mockSmarty);
110
$this->assertEquals(array(array('foo' => 'bar'), array('forceFullUrl' => true)),
111
$this->_mockUrlGenerator->_calls[0]);
114
function testForceFullUrlOff() {
115
$this->_setupAdapterWithMocks();
116
$this->_templateAdapter->url(array('arg1' => 'foo=bar'), $this->_mockSmarty);
118
$this->assertEquals(array(array('foo' => 'bar'), array()),
119
$this->_mockUrlGenerator->_calls[0]);
122
function testUrlParamsAndArgs() {
123
$this->_setupAdapterWithMocks();
124
$this->_templateAdapter->url(array('params' => array('view' => 'fun.View'),
125
'arg1' => 'foo=bar'), $this->_mockSmarty);
127
$this->assertEquals(array(array('view' => 'fun.View', 'foo' => 'bar'), array()),
128
$this->_mockUrlGenerator->_calls[0]);
131
function testUrlOptionsAndBulkOptions() {
132
$this->_setupAdapterWithMocks();
133
$this->_templateAdapter->url(array('options' => array('foo' => 'foo', 'bar' => 'bar'),
134
'foo' => 'bar'), $this->_mockSmarty);
136
$this->assertEquals(array(array(), array('foo' => 'bar', 'bar' => 'bar')),
137
$this->_mockUrlGenerator->_calls[0]);
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);
149
/* Make sure viewL10domain doesn't get ignored */
150
function testTextViewL10domain() {
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'),
159
$this->assertEquals('Test passed', $translated);
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'),
170
function testThemeUrlsWithOverride() {
172
$this->_setupAdapterWithMocks();
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;
180
$results = $this->_templateAdapter->theme(
181
array('url' => 'foo.css'), $this->_mockSmarty);
182
$this->assertEquals(array('href' => 'themes/mock/local/foo.css'),
186
function testThemeUrlsWithOverrideSubdir() {
188
$this->_setupAdapterWithMocks();
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;
198
$results = $this->_templateAdapter->theme(
199
array('url' => 'templates/foo.css'), $this->_mockSmarty);
200
$this->assertEquals(array('href' => 'themes/mock/templates/local/foo.css'),
204
function testAutoCompleteIncludesAuthToken() {
206
$session =& $gallery->getSession();
207
$this->_setupAdapterWithMocks();
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();
215
array('smarty_include_tpl_file' => 'gallery:modules/core/templates/AutoComplete.tpl',
216
'smarty_include_vars' => array('element' => 'giFormUsername',
217
'url' => $expectedUrl,
219
$this->_mockSmarty->_includes[0]);
223
class TemplateAdapterMockTheme {
229
class TemplateAdapterMockSmarty {
230
var $_includes = array();
232
function _smarty_include($params) {
233
$this->_includes[] = $params;
237
class TemplateAdapterMockUrlGenerator {
238
function generateUrl($params=array(), $options=array()) {
239
$this->_calls[] = array($params, $options);
244
class TemplateAdapterMockTranslator {
245
function translateDomain($domain, $params) {
246
if ($domain == 'te_TE' && $params['text'] == 'Test failed') {
247
return array(null, 'Test passed');
249
return array(null, null);