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");
7
require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
8
require_once "PHPUnit/Framework/TestCase.php";
9
require_once "PHPUnit/Framework/TestSuite.php";
11
/** Zend_View_Helper_HeadLink */
12
require_once 'Zend/View/Helper/HeadLink.php';
14
/** Zend_View_Helper_Placeholder_Registry */
15
require_once 'Zend/View/Helper/Placeholder/Registry.php';
18
require_once 'Zend/Registry.php';
21
require_once 'Zend/View.php';
24
* Test class for Zend_View_Helper_HeadLink.
28
* @subpackage UnitTests
30
class Zend_View_Helper_HeadLinkTest extends PHPUnit_Framework_TestCase
33
* @var Zend_View_Helper_HeadLink
43
* Runs the test methods of this class.
47
public static function main()
49
require_once "PHPUnit/TextUI/TestRunner.php";
51
$suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadLinkTest");
52
$result = PHPUnit_TextUI_TestRunner::run($suite);
56
* Sets up the fixture, for example, open a network connection.
57
* This method is called before a test is executed.
61
public function setUp()
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]);
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);
76
* Tears down the fixture, for example, close a network connection.
77
* This method is called after a test is executed.
81
public function tearDown()
86
public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
88
$registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
89
if ($registry->containerExists('Zend_View_Helper_HeadLink')) {
90
$registry->deleteContainer('Zend_View_Helper_HeadLink');
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'));
97
public function testHeadLinkReturnsObjectInstance()
99
$placeholder = $this->helper->headLink();
100
$this->assertTrue($placeholder instanceof Zend_View_Helper_HeadLink);
103
public function testPrependThrowsExceptionWithoutArrayArgument()
106
$this->helper->prepend('foo');
107
$this->fail('prepend should raise exception without array argument');
108
} catch (Exception $e) {
112
public function testAppendThrowsExceptionWithoutArrayArgument()
115
$this->helper->append('foo');
116
$this->fail('append should raise exception without array argument');
117
} catch (Exception $e) {
121
public function testSetThrowsExceptionWithoutArrayArgument()
124
$this->helper->set('foo');
125
$this->fail('set should raise exception without array argument');
126
} catch (Exception $e) {
130
public function testOffsetSetThrowsExceptionWithoutArrayArgument()
133
$this->helper->offsetSet(1, 'foo');
134
$this->fail('set should raise exception without array argument');
135
} catch (Exception $e) {
139
public function testCreatingLinkStackViaHeadScriptCreatesAppropriateOutput()
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'),
146
$this->helper->headLink($links['link1'])
147
->headLink($links['link2'], 'PREPEND')
148
->headLink($links['link3']);
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);
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);
166
foreach ($this->helper as $key => $value) {
167
if (isset($value->href)) {
168
$order[$key] = $value->href;
171
$expected = array('bar', 'foo', 'baz');
172
$this->assertSame($expected, $order);
175
public function testCreatingLinkStackViaStyleSheetMethodsCreatesAppropriateOutput()
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'),
182
$this->helper->appendStylesheet($links['link1']['href'])
183
->prependStylesheet($links['link2']['href'])
184
->appendStylesheet($links['link3']['href']);
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);
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);
202
foreach ($this->helper as $key => $value) {
203
if (isset($value->href)) {
204
$order[$key] = $value->href;
207
$expected = array('bar', 'foo', 'baz');
208
$this->assertSame($expected, $order);
211
public function testCreatingLinkStackViaAlternateMethodsCreatesAppropriateOutput()
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'),
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';
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);
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);
243
foreach ($this->helper as $key => $value) {
244
if (isset($value->href)) {
245
$order[$key] = $value->href;
248
$expected = array('bar', 'foo', 'baz');
249
$this->assertSame($expected, $order);
252
public function testOverloadingThrowsExceptionWithNoArguments()
255
$this->helper->appendStylesheet();
256
$this->fail('Helper should expect at least one argument');
257
} catch (Zend_View_Exception $e) {}
260
public function testOverloadingShouldAllowSingleArrayArgument()
262
$this->helper->setStylesheet(array('href' => '/styles.css'));
263
$link = $this->helper->getValue();
264
$this->assertEquals('/styles.css', $link->href);
267
public function testOverloadingUsingSingleArrayArgumentWithInvalidValuesThrowsException()
270
$this->helper->setStylesheet(array('bogus' => 'unused'));
271
$this->fail('Invalid attribute values should raise exception');
272
} catch (Zend_View_Exception $e) { }
275
public function testOverloadingOffsetSetWorks()
277
$this->helper->offsetSetStylesheet(100, '/styles.css');
278
$items = $this->helper->getArrayCopy();
279
$this->assertTrue(isset($items[100]));
281
$this->assertEquals('/styles.css', $link->href);
284
public function testOverloadingThrowsExceptionWithInvalidMethod()
287
$this->helper->bogusMethod();
288
$this->fail('Invalid method should raise exception');
289
} catch (Zend_View_Exception $e) { }
292
public function testStylesheetAttributesGetSet()
294
$this->helper->setStylesheet('/styles.css', 'projection', 'ie6');
295
$item = $this->helper->getValue();
296
$this->assertObjectHasAttribute('media', $item);
297
$this->assertObjectHasAttribute('conditionalStylesheet', $item);
299
$this->assertEquals('projection', $item->media);
300
$this->assertEquals('ie6', $item->conditionalStylesheet);
303
public function testConditionalStylesheetNotCreatedByDefault()
305
$this->helper->setStylesheet('/styles.css');
306
$item = $this->helper->getValue();
307
$this->assertObjectHasAttribute('conditionalStylesheet', $item);
308
$this->assertFalse($item->conditionalStylesheet);
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);
317
public function testConditionalStylesheetCreationOccursWhenRequested()
319
$this->helper->setStylesheet('/styles.css', 'screen', 'ie6');
320
$item = $this->helper->getValue();
321
$this->assertObjectHasAttribute('conditionalStylesheet', $item);
322
$this->assertEquals('ie6', $item->conditionalStylesheet);
324
$string = $this->helper->toString();
325
$this->assertContains('/styles.css', $string);
326
$this->assertContains('<!--[if ie6]>', $string);
327
$this->assertContains('<![endif]-->', $string);
330
public function testSettingAlternateWithTooFewArgsRaisesException()
333
$this->helper->setAlternate('foo');
334
$this->fail('Setting alternate with fewer than 3 args should raise exception');
335
} catch (Zend_View_Exception $e) { }
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) { }
342
public function testIndentationIsHonored()
344
$this->helper->setIndent(4);
345
$this->helper->appendStylesheet('/css/screen.css');
346
$this->helper->appendStylesheet('/css/rules.css');
347
$string = $this->helper->toString();
349
$scripts = substr_count($string, ' <link ');
350
$this->assertEquals(2, $scripts);
353
public function testLinkRendersAsPlainHtmlIfDoctypeNotXhtml()
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);
362
public function testDoesNotAllowDuplicateStylesheets()
364
$this->helper->appendStylesheet('foo');
365
$this->helper->appendStylesheet('foo');
366
$this->assertEquals(1, count($this->helper), var_export($this->helper->getContainer()->getArrayCopy(), 1));
372
public function testBooleanStylesheet()
374
$this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => false));
375
$test = $this->helper->toString();
376
$this->assertNotContains('[if false]', $test);
383
public function testBooleanTrueConditionalStylesheet()
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);
393
* @link http://framework.zend.com/issues/browse/ZF-3928
395
public function testTurnOffAutoEscapeDoesNotEncodeAmpersand()
397
$this->helper->setAutoEscape(false)->appendStylesheet('/css/rules.css?id=123&foo=bar');
398
$this->assertContains('id=123&foo=bar', $this->helper->toString());
401
public function testSetAlternateWithExtras()
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);
408
public function testAppendStylesheetWithExtras()
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);
415
public function testSetStylesheetWithMediaAsArray()
417
$this->helper->appendStylesheet('/bar/baz', array('screen','print'));
418
$test = $this->helper->toString();
419
$this->assertContains(' media="screen,print"', $test);
425
public function testContainerMaintainsCorrectOrderOfItems()
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');
432
$test = $this->helper->toString();
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" >';
439
$this->assertEquals($expected, $test);
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();