~clinton-collins/familyproject/trunk

« back to all changes in this revision

Viewing changes to ZendFramework/tests/Zend/View/Helper/HeadLinkTest.php

  • Committer: Clinton Collins
  • Date: 2009-06-26 19:54:58 UTC
  • Revision ID: clinton.collins@gmail.com-20090626195458-5ebba0qcvo15xlpy
Initial Import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// Call Zend_View_Helper_HeadLinkTest::main() if this source file is executed directly.
 
3
if (!defined("PHPUnit_MAIN_METHOD")) {
 
4
    define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadLinkTest::main");
 
5
}
 
6
 
 
7
require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
 
8
require_once "PHPUnit/Framework/TestCase.php";
 
9
require_once "PHPUnit/Framework/TestSuite.php";
 
10
 
 
11
/** Zend_View_Helper_HeadLink */
 
12
require_once 'Zend/View/Helper/HeadLink.php';
 
13
 
 
14
/** Zend_View_Helper_Placeholder_Registry */
 
15
require_once 'Zend/View/Helper/Placeholder/Registry.php';
 
16
 
 
17
/** Zend_Registry */
 
18
require_once 'Zend/Registry.php';
 
19
 
 
20
/** Zend_View */
 
21
require_once 'Zend/View.php';
 
22
 
 
23
/**
 
24
 * Test class for Zend_View_Helper_HeadLink.
 
25
 *
 
26
 * @category   Zend
 
27
 * @package    Zend_View
 
28
 * @subpackage UnitTests
 
29
 */
 
30
class Zend_View_Helper_HeadLinkTest extends PHPUnit_Framework_TestCase
 
31
{
 
32
    /**
 
33
     * @var Zend_View_Helper_HeadLink
 
34
     */
 
35
    public $helper;
 
36
 
 
37
    /**
 
38
     * @var string
 
39
     */
 
40
    public $basePath;
 
41
 
 
42
    /**
 
43
     * Runs the test methods of this class.
 
44
     *
 
45
     * @return void
 
46
     */
 
47
    public static function main()
 
48
    {
 
49
        require_once "PHPUnit/TextUI/TestRunner.php";
 
50
 
 
51
        $suite  = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadLinkTest");
 
52
        $result = PHPUnit_TextUI_TestRunner::run($suite);
 
53
    }
 
54
 
 
55
    /**
 
56
     * Sets up the fixture, for example, open a network connection.
 
57
     * This method is called before a test is executed.
 
58
     *
 
59
     * @return void
 
60
     */
 
61
    public function setUp()
 
62
    {
 
63
        foreach (array(Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY, 'Zend_View_Helper_Doctype') as $key) {
 
64
            if (Zend_Registry::isRegistered($key)) {
 
65
                $registry = Zend_Registry::getInstance();
 
66
                unset($registry[$key]);
 
67
            }
 
68
        }
 
69
        $this->basePath = dirname(__FILE__) . '/_files/modules';
 
70
        $this->view = new Zend_View();
 
71
        $this->helper = new Zend_View_Helper_HeadLink();
 
72
        $this->helper->setView($this->view);
 
73
    }
 
74
 
 
75
    /**
 
76
     * Tears down the fixture, for example, close a network connection.
 
77
     * This method is called after a test is executed.
 
78
     *
 
79
     * @return void
 
80
     */
 
81
    public function tearDown()
 
82
    {
 
83
        unset($this->helper);
 
84
    }
 
85
 
 
86
    public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
 
87
    {
 
88
        $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
 
89
        if ($registry->containerExists('Zend_View_Helper_HeadLink')) {
 
90
            $registry->deleteContainer('Zend_View_Helper_HeadLink');
 
91
        }
 
92
        $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadLink'));
 
93
        $helper = new Zend_View_Helper_HeadLink();
 
94
        $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadLink'));
 
95
    }
 
96
 
 
97
    public function testHeadLinkReturnsObjectInstance()
 
98
    {
 
99
        $placeholder = $this->helper->headLink();
 
100
        $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadLink);
 
101
    }
 
102
 
 
103
    public function testPrependThrowsExceptionWithoutArrayArgument()
 
104
    {
 
105
        try {
 
106
            $this->helper->prepend('foo');
 
107
            $this->fail('prepend should raise exception without array argument');
 
108
        } catch (Exception $e) {
 
109
        }
 
110
    }
 
111
 
 
112
    public function testAppendThrowsExceptionWithoutArrayArgument()
 
113
    {
 
114
        try {
 
115
            $this->helper->append('foo');
 
116
            $this->fail('append should raise exception without array argument');
 
117
        } catch (Exception $e) {
 
118
        }
 
119
    }
 
120
 
 
121
    public function testSetThrowsExceptionWithoutArrayArgument()
 
122
    {
 
123
        try {
 
124
            $this->helper->set('foo');
 
125
            $this->fail('set should raise exception without array argument');
 
126
        } catch (Exception $e) {
 
127
        }
 
128
    }
 
129
 
 
130
    public function testOffsetSetThrowsExceptionWithoutArrayArgument()
 
131
    {
 
132
        try {
 
133
            $this->helper->offsetSet(1, 'foo');
 
134
            $this->fail('set should raise exception without array argument');
 
135
        } catch (Exception $e) {
 
136
        }
 
137
    }
 
138
 
 
139
    public function testCreatingLinkStackViaHeadScriptCreatesAppropriateOutput()
 
140
    {
 
141
        $links = array(
 
142
            'link1' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
 
143
            'link2' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
 
144
            'link3' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
 
145
        );
 
146
        $this->helper->headLink($links['link1'])
 
147
                     ->headLink($links['link2'], 'PREPEND')
 
148
                     ->headLink($links['link3']);
 
149
 
 
150
        $string = $this->helper->toString();
 
151
        $lines  = substr_count($string, PHP_EOL);
 
152
        $this->assertEquals(2, $lines);
 
153
        $lines  = substr_count($string, '<link ');
 
154
        $this->assertEquals(3, $lines, $string);
 
155
 
 
156
        foreach ($links as $link) {
 
157
            $substr = ' href="' . $link['href'] . '"';
 
158
            $this->assertContains($substr, $string);
 
159
            $substr = ' rel="' . $link['rel'] . '"';
 
160
            $this->assertContains($substr, $string);
 
161
            $substr = ' type="' . $link['type'] . '"';
 
162
            $this->assertContains($substr, $string);
 
163
        }
 
164
 
 
165
        $order = array();
 
166
        foreach ($this->helper as $key => $value) {
 
167
            if (isset($value->href)) {
 
168
                $order[$key] = $value->href;
 
169
            }
 
170
        }
 
171
        $expected = array('bar', 'foo', 'baz');
 
172
        $this->assertSame($expected, $order);
 
173
    }
 
174
 
 
175
    public function testCreatingLinkStackViaStyleSheetMethodsCreatesAppropriateOutput()
 
176
    {
 
177
        $links = array(
 
178
            'link1' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
 
179
            'link2' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
 
180
            'link3' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
 
181
        );
 
182
        $this->helper->appendStylesheet($links['link1']['href'])
 
183
                     ->prependStylesheet($links['link2']['href'])
 
184
                     ->appendStylesheet($links['link3']['href']);
 
185
 
 
186
        $string = $this->helper->toString();
 
187
        $lines  = substr_count($string, PHP_EOL);
 
188
        $this->assertEquals(2, $lines);
 
189
        $lines  = substr_count($string, '<link ');
 
190
        $this->assertEquals(3, $lines, $string);
 
191
 
 
192
        foreach ($links as $link) {
 
193
            $substr = ' href="' . $link['href'] . '"';
 
194
            $this->assertContains($substr, $string);
 
195
            $substr = ' rel="' . $link['rel'] . '"';
 
196
            $this->assertContains($substr, $string);
 
197
            $substr = ' type="' . $link['type'] . '"';
 
198
            $this->assertContains($substr, $string);
 
199
        }
 
200
 
 
201
        $order = array();
 
202
        foreach ($this->helper as $key => $value) {
 
203
            if (isset($value->href)) {
 
204
                $order[$key] = $value->href;
 
205
            }
 
206
        }
 
207
        $expected = array('bar', 'foo', 'baz');
 
208
        $this->assertSame($expected, $order);
 
209
    }
 
210
 
 
211
    public function testCreatingLinkStackViaAlternateMethodsCreatesAppropriateOutput()
 
212
    {
 
213
        $links = array(
 
214
            'link1' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
 
215
            'link2' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
 
216
            'link3' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
 
217
        );
 
218
        $where = 'append';
 
219
        foreach ($links as $link) {
 
220
            $method = $where . 'Alternate';
 
221
            $this->helper->$method($link['href'], $link['type'], $link['title']);
 
222
            $where = ('append' == $where) ? 'prepend' : 'append';
 
223
        }
 
224
 
 
225
        $string = $this->helper->toString();
 
226
        $lines  = substr_count($string, PHP_EOL);
 
227
        $this->assertEquals(2, $lines);
 
228
        $lines  = substr_count($string, '<link ');
 
229
        $this->assertEquals(3, $lines, $string);
 
230
        $lines  = substr_count($string, ' rel="alternate"');
 
231
        $this->assertEquals(3, $lines, $string);
 
232
 
 
233
        foreach ($links as $link) {
 
234
            $substr = ' href="' . $link['href'] . '"';
 
235
            $this->assertContains($substr, $string);
 
236
            $substr = ' title="' . $link['title'] . '"';
 
237
            $this->assertContains($substr, $string);
 
238
            $substr = ' type="' . $link['type'] . '"';
 
239
            $this->assertContains($substr, $string);
 
240
        }
 
241
 
 
242
        $order = array();
 
243
        foreach ($this->helper as $key => $value) {
 
244
            if (isset($value->href)) {
 
245
                $order[$key] = $value->href;
 
246
            }
 
247
        }
 
248
        $expected = array('bar', 'foo', 'baz');
 
249
        $this->assertSame($expected, $order);
 
250
    }
 
251
 
 
252
    public function testOverloadingThrowsExceptionWithNoArguments()
 
253
    {
 
254
        try {
 
255
            $this->helper->appendStylesheet();
 
256
            $this->fail('Helper should expect at least one argument');
 
257
        } catch (Zend_View_Exception $e) {}
 
258
    }
 
259
 
 
260
    public function testOverloadingShouldAllowSingleArrayArgument()
 
261
    {
 
262
        $this->helper->setStylesheet(array('href' => '/styles.css'));
 
263
        $link = $this->helper->getValue();
 
264
        $this->assertEquals('/styles.css', $link->href);
 
265
    }
 
266
 
 
267
    public function testOverloadingUsingSingleArrayArgumentWithInvalidValuesThrowsException()
 
268
    {
 
269
        try {
 
270
            $this->helper->setStylesheet(array('bogus' => 'unused'));
 
271
            $this->fail('Invalid attribute values should raise exception');
 
272
        } catch (Zend_View_Exception $e) { }
 
273
    }
 
274
 
 
275
    public function testOverloadingOffsetSetWorks()
 
276
    {
 
277
        $this->helper->offsetSetStylesheet(100, '/styles.css');
 
278
        $items = $this->helper->getArrayCopy();
 
279
        $this->assertTrue(isset($items[100]));
 
280
        $link = $items[100];
 
281
        $this->assertEquals('/styles.css', $link->href);
 
282
    }
 
283
 
 
284
    public function testOverloadingThrowsExceptionWithInvalidMethod()
 
285
    {
 
286
        try {
 
287
            $this->helper->bogusMethod();
 
288
            $this->fail('Invalid method should raise exception');
 
289
        } catch (Zend_View_Exception $e) { }
 
290
    }
 
291
 
 
292
    public function testStylesheetAttributesGetSet()
 
293
    {
 
294
        $this->helper->setStylesheet('/styles.css', 'projection', 'ie6');
 
295
        $item = $this->helper->getValue();
 
296
        $this->assertObjectHasAttribute('media', $item);
 
297
        $this->assertObjectHasAttribute('conditionalStylesheet', $item);
 
298
 
 
299
        $this->assertEquals('projection', $item->media);
 
300
        $this->assertEquals('ie6', $item->conditionalStylesheet);
 
301
    }
 
302
 
 
303
    public function testConditionalStylesheetNotCreatedByDefault()
 
304
    {
 
305
        $this->helper->setStylesheet('/styles.css');
 
306
        $item = $this->helper->getValue();
 
307
        $this->assertObjectHasAttribute('conditionalStylesheet', $item);
 
308
        $this->assertFalse($item->conditionalStylesheet);
 
309
 
 
310
        $string = $this->helper->toString();
 
311
        $this->assertContains('/styles.css', $string);
 
312
        $this->assertNotContains('<!--[if', $string);
 
313
        $this->assertNotContains(']>', $string);
 
314
        $this->assertNotContains('<![endif]-->', $string);
 
315
    }
 
316
 
 
317
    public function testConditionalStylesheetCreationOccursWhenRequested()
 
318
    {
 
319
        $this->helper->setStylesheet('/styles.css', 'screen', 'ie6');
 
320
        $item = $this->helper->getValue();
 
321
        $this->assertObjectHasAttribute('conditionalStylesheet', $item);
 
322
        $this->assertEquals('ie6', $item->conditionalStylesheet);
 
323
 
 
324
        $string = $this->helper->toString();
 
325
        $this->assertContains('/styles.css', $string);
 
326
        $this->assertContains('<!--[if ie6]>', $string);
 
327
        $this->assertContains('<![endif]-->', $string);
 
328
    }
 
329
 
 
330
    public function testSettingAlternateWithTooFewArgsRaisesException()
 
331
    {
 
332
        try {
 
333
            $this->helper->setAlternate('foo');
 
334
            $this->fail('Setting alternate with fewer than 3 args should raise exception');
 
335
        } catch (Zend_View_Exception $e) { }
 
336
        try {
 
337
            $this->helper->setAlternate('foo', 'bar');
 
338
            $this->fail('Setting alternate with fewer than 3 args should raise exception');
 
339
        } catch (Zend_View_Exception $e) { }
 
340
    }
 
341
 
 
342
    public function testIndentationIsHonored()
 
343
    {
 
344
        $this->helper->setIndent(4);
 
345
        $this->helper->appendStylesheet('/css/screen.css');
 
346
        $this->helper->appendStylesheet('/css/rules.css');
 
347
        $string = $this->helper->toString();
 
348
 
 
349
        $scripts = substr_count($string, '    <link ');
 
350
        $this->assertEquals(2, $scripts);
 
351
    }
 
352
 
 
353
    public function testLinkRendersAsPlainHtmlIfDoctypeNotXhtml()
 
354
    {
 
355
        $this->view->doctype('HTML4_STRICT');
 
356
        $this->helper->headLink(array('rel' => 'icon', 'src' => '/foo/bar'))
 
357
                     ->headLink(array('rel' => 'foo', 'href' => '/bar/baz'));
 
358
        $test = $this->helper->toString();
 
359
        $this->assertNotContains(' />', $test);
 
360
    }
 
361
 
 
362
    public function testDoesNotAllowDuplicateStylesheets()
 
363
    {
 
364
        $this->helper->appendStylesheet('foo');
 
365
        $this->helper->appendStylesheet('foo');
 
366
        $this->assertEquals(1, count($this->helper), var_export($this->helper->getContainer()->getArrayCopy(), 1));
 
367
    }
 
368
 
 
369
    /**
 
370
     * test for ZF-2889
 
371
     */
 
372
    public function testBooleanStylesheet()
 
373
    {
 
374
        $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => false));
 
375
        $test = $this->helper->toString();
 
376
        $this->assertNotContains('[if false]', $test);
 
377
    }
 
378
 
 
379
    /**
 
380
     * test for ZF-3271
 
381
     *
 
382
     */
 
383
    public function testBooleanTrueConditionalStylesheet()
 
384
    {
 
385
        $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => true));
 
386
        $test = $this->helper->toString();
 
387
        $this->assertNotContains('[if 1]', $test);
 
388
        $this->assertNotContains('[if true]', $test);
 
389
    }
 
390
 
 
391
    /**
 
392
     * @issue ZF-3928
 
393
     * @link http://framework.zend.com/issues/browse/ZF-3928
 
394
     */
 
395
    public function testTurnOffAutoEscapeDoesNotEncodeAmpersand()
 
396
    {
 
397
        $this->helper->setAutoEscape(false)->appendStylesheet('/css/rules.css?id=123&foo=bar');
 
398
        $this->assertContains('id=123&foo=bar', $this->helper->toString());
 
399
    }
 
400
 
 
401
    public function testSetAlternateWithExtras()
 
402
    {
 
403
        $this->helper->setAlternate('/mydocument.pdf', 'application/pdf', 'foo', array('media' => array('print','screen')));
 
404
        $test = $this->helper->toString();
 
405
        $this->assertContains('media="print,screen"', $test);
 
406
    }
 
407
 
 
408
    public function testAppendStylesheetWithExtras()
 
409
    {
 
410
        $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => false, 'extras' => array('id' => 'my_link_tag')));
 
411
        $test = $this->helper->toString();
 
412
        $this->assertContains('id="my_link_tag"', $test);
 
413
    }
 
414
 
 
415
    public function testSetStylesheetWithMediaAsArray()
 
416
    {
 
417
        $this->helper->appendStylesheet('/bar/baz', array('screen','print'));
 
418
        $test = $this->helper->toString();
 
419
        $this->assertContains(' media="screen,print"', $test);
 
420
    }
 
421
 
 
422
    /**
 
423
     * @issue ZF-5435
 
424
     */
 
425
    public function testContainerMaintainsCorrectOrderOfItems()
 
426
    {
 
427
        $this->helper->headLink()->offsetSetStylesheet(1,'/test1.css');
 
428
        $this->helper->headLink()->offsetSetStylesheet(10,'/test2.css');
 
429
        $this->helper->headLink()->offsetSetStylesheet(20,'/test3.css');
 
430
        $this->helper->headLink()->offsetSetStylesheet(5,'/test4.css');
 
431
 
 
432
        $test = $this->helper->toString();
 
433
 
 
434
        $expected = '<link href="/test1.css" media="screen" rel="stylesheet" type="text/css" >
 
435
<link href="/test4.css" media="screen" rel="stylesheet" type="text/css" >
 
436
<link href="/test2.css" media="screen" rel="stylesheet" type="text/css" >
 
437
<link href="/test3.css" media="screen" rel="stylesheet" type="text/css" >';
 
438
 
 
439
        $this->assertEquals($expected, $test);
 
440
    }
 
441
}
 
442
 
 
443
// Call Zend_View_Helper_HeadLinkTest::main() if this source file is executed directly.
 
444
if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadLinkTest::main") {
 
445
    Zend_View_Helper_HeadLinkTest::main();
 
446
}