2013-08-14 21:37:24 +10:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DebugBar\Tests;
|
|
|
|
|
|
|
|
use DebugBar\JavascriptRenderer;
|
|
|
|
use DebugBar\DebugBar;
|
|
|
|
use DebugBar\StandardDebugBar;
|
|
|
|
|
|
|
|
class JavascriptRendererTest extends DebugBarTestCase
|
|
|
|
{
|
2020-12-07 11:39:43 +01:00
|
|
|
public function setUp(): void
|
2013-08-14 21:37:24 +10:00
|
|
|
{
|
2013-08-14 22:12:33 +10:00
|
|
|
parent::setUp();
|
2013-08-14 21:37:24 +10:00
|
|
|
$this->r = new JavascriptRenderer($this->debugbar);
|
2014-03-22 15:54:52 -04:00
|
|
|
$this->r->setBasePath('/bpath');
|
|
|
|
$this->r->setBaseUrl('/burl');
|
2013-08-14 21:37:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOptions()
|
|
|
|
{
|
|
|
|
$this->r->setOptions(array(
|
|
|
|
'base_path' => '/foo',
|
|
|
|
'base_url' => '/foo',
|
|
|
|
'include_vendors' => false,
|
|
|
|
'javascript_class' => 'Foobar',
|
|
|
|
'variable_name' => 'foovar',
|
|
|
|
'initialization' => JavascriptRenderer::INITIALIZE_CONTROLS,
|
2013-09-19 16:31:50 -04:00
|
|
|
'enable_jquery_noconflict' => true,
|
2013-08-14 21:37:24 +10:00
|
|
|
'controls' => array(
|
|
|
|
'memory' => array(
|
2013-08-16 00:52:32 +01:00
|
|
|
"icon" => "cogs",
|
|
|
|
"map" => "memory.peak_usage_str",
|
2013-08-14 21:37:24 +10:00
|
|
|
"default" => "'0B'"
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'disable_controls' => array('messages'),
|
2013-09-19 16:31:50 -04:00
|
|
|
'ignore_collectors' => 'config',
|
|
|
|
'ajax_handler_classname' => 'AjaxFoo',
|
|
|
|
'ajax_handler_bind_to_jquery' => false,
|
2017-03-04 23:17:18 -08:00
|
|
|
'ajax_handler_auto_show' => false,
|
2013-09-19 16:31:50 -04:00
|
|
|
'open_handler_classname' => 'OpenFoo',
|
|
|
|
'open_handler_url' => 'open.php'
|
2013-08-14 21:37:24 +10:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals('/foo', $this->r->getBasePath());
|
|
|
|
$this->assertEquals('/foo', $this->r->getBaseUrl());
|
|
|
|
$this->assertFalse($this->r->areVendorsIncluded());
|
|
|
|
$this->assertEquals('Foobar', $this->r->getJavascriptClass());
|
|
|
|
$this->assertEquals('foovar', $this->r->getVariableName());
|
|
|
|
$this->assertEquals(JavascriptRenderer::INITIALIZE_CONTROLS, $this->r->getInitialization());
|
2013-09-19 16:31:50 -04:00
|
|
|
$this->assertTrue($this->r->isJqueryNoConflictEnabled());
|
2013-08-14 21:37:24 +10:00
|
|
|
$controls = $this->r->getControls();
|
|
|
|
$this->assertCount(2, $controls);
|
|
|
|
$this->assertArrayHasKey('memory', $controls);
|
|
|
|
$this->assertArrayHasKey('messages', $controls);
|
|
|
|
$this->assertNull($controls['messages']);
|
|
|
|
$this->assertContains('config', $this->r->getIgnoredCollectors());
|
2013-09-19 16:31:50 -04:00
|
|
|
$this->assertEquals('AjaxFoo', $this->r->getAjaxHandlerClass());
|
|
|
|
$this->assertFalse($this->r->isAjaxHandlerBoundToJquery());
|
2017-03-04 23:17:18 -08:00
|
|
|
$this->assertFalse($this->r->isAjaxHandlerAutoShow());
|
2013-09-19 16:31:50 -04:00
|
|
|
$this->assertEquals('OpenFoo', $this->r->getOpenHandlerClass());
|
|
|
|
$this->assertEquals('open.php', $this->r->getOpenHandlerUrl());
|
2013-08-14 21:37:24 +10:00
|
|
|
}
|
|
|
|
|
2014-03-22 15:54:52 -04:00
|
|
|
public function testAddAssets()
|
|
|
|
{
|
2017-07-15 02:08:32 -07:00
|
|
|
// Use a loop to test deduplication of assets
|
|
|
|
for ($i = 0; $i < 2; ++$i) {
|
|
|
|
$this->r->addAssets('foo.css', 'foo.js', '/bar', '/foobar');
|
|
|
|
$this->r->addInlineAssets(array('Css' => 'CssTest'), array('Js' => 'JsTest'), array('Head' => 'HeaderTest'));
|
|
|
|
}
|
2014-03-22 15:54:52 -04:00
|
|
|
|
2017-07-15 02:08:32 -07:00
|
|
|
// Make sure all the right assets are returned by getAssets
|
|
|
|
list($css, $js, $inline_css, $inline_js, $inline_head) = $this->r->getAssets();
|
2014-03-22 15:54:52 -04:00
|
|
|
$this->assertContains('/bar/foo.css', $css);
|
|
|
|
$this->assertContains('/bar/foo.js', $js);
|
2017-07-15 02:08:32 -07:00
|
|
|
$this->assertEquals(array('Css' => 'CssTest'), $inline_css);
|
|
|
|
$this->assertEquals(array('Js' => 'JsTest'), $inline_js);
|
|
|
|
$this->assertEquals(array('Head' => 'HeaderTest'), $inline_head);
|
|
|
|
|
|
|
|
// Make sure asset files are deduplicated
|
|
|
|
$this->assertCount(count(array_unique($css)), $css);
|
|
|
|
$this->assertCount(count(array_unique($js)), $js);
|
2014-03-22 15:54:52 -04:00
|
|
|
|
|
|
|
$html = $this->r->renderHead();
|
2020-12-07 11:39:43 +01:00
|
|
|
$this->assertStringContainsString('<script type="text/javascript" src="/foobar/foo.js"></script>', $html);
|
2014-03-22 15:54:52 -04:00
|
|
|
}
|
|
|
|
|
2013-08-14 21:37:24 +10:00
|
|
|
public function testGetAssets()
|
|
|
|
{
|
|
|
|
list($css, $js) = $this->r->getAssets();
|
2014-03-22 15:54:52 -04:00
|
|
|
$this->assertContains('/bpath/debugbar.css', $css);
|
|
|
|
$this->assertContains('/bpath/widgets.js', $js);
|
2014-03-22 22:27:25 -04:00
|
|
|
$this->assertContains('/bpath/vendor/jquery/dist/jquery.min.js', $js);
|
2013-08-14 21:37:24 +10:00
|
|
|
|
|
|
|
$this->r->setIncludeVendors(false);
|
|
|
|
$js = $this->r->getAssets('js');
|
2014-03-22 15:54:52 -04:00
|
|
|
$this->assertContains('/bpath/debugbar.js', $js);
|
2014-03-22 22:27:25 -04:00
|
|
|
$this->assertNotContains('/bpath/vendor/jquery/dist/jquery.min.js', $js);
|
2013-08-14 21:37:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenderHead()
|
|
|
|
{
|
2017-07-15 02:08:32 -07:00
|
|
|
$this->r->addInlineAssets(array('Css' => 'CssTest'), array('Js' => 'JsTest'), array('Head' => 'HeaderTest'));
|
|
|
|
|
2013-08-14 21:37:24 +10:00
|
|
|
$html = $this->r->renderHead();
|
2017-07-15 02:08:32 -07:00
|
|
|
// Check for file links
|
2020-12-07 11:39:43 +01:00
|
|
|
$this->assertStringContainsString('<link rel="stylesheet" type="text/css" href="/burl/debugbar.css">', $html);
|
|
|
|
$this->assertStringContainsString('<script type="text/javascript" src="/burl/debugbar.js"></script>', $html);
|
2017-07-15 02:08:32 -07:00
|
|
|
// Check for inline assets
|
2020-12-07 11:39:43 +01:00
|
|
|
$this->assertStringContainsString('<style type="text/css">CssTest</style>', $html);
|
|
|
|
$this->assertStringContainsString('<script type="text/javascript">JsTest</script>', $html);
|
|
|
|
$this->assertStringContainsString('HeaderTest', $html);
|
2017-07-15 02:08:32 -07:00
|
|
|
// Check jQuery noConflict
|
2020-12-07 11:39:43 +01:00
|
|
|
$this->assertStringContainsString('jQuery.noConflict(true);', $html);
|
2014-07-26 15:36:05 +02:00
|
|
|
|
2017-07-15 02:08:32 -07:00
|
|
|
// Check for absence of jQuery noConflict
|
2014-07-26 15:36:05 +02:00
|
|
|
$this->r->setEnableJqueryNoConflict(false);
|
|
|
|
$html = $this->r->renderHead();
|
2020-12-07 11:39:43 +01:00
|
|
|
$this->assertStringNotContainsString('noConflict', $html);
|
2013-08-14 21:37:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenderFullInitialization()
|
|
|
|
{
|
|
|
|
$this->debugbar->addCollector(new \DebugBar\DataCollector\MessagesCollector());
|
|
|
|
$this->r->addControl('time', array('icon' => 'time', 'map' => 'time', 'default' => '"0s"'));
|
2014-01-17 19:58:09 +00:00
|
|
|
$expected = rtrim(file_get_contents(__DIR__ . '/full_init.html'));
|
2013-08-14 22:12:33 +10:00
|
|
|
$this->assertStringStartsWith($expected, $this->r->render());
|
2013-08-14 21:37:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenderConstructorOnly()
|
|
|
|
{
|
|
|
|
$this->r->setInitialization(JavascriptRenderer::INITIALIZE_CONSTRUCTOR);
|
|
|
|
$this->r->setJavascriptClass('Foobar');
|
|
|
|
$this->r->setVariableName('foovar');
|
2013-09-15 11:40:37 -04:00
|
|
|
$this->r->setAjaxHandlerClass(false);
|
2014-07-26 15:36:05 +02:00
|
|
|
$this->assertStringStartsWith("<script type=\"text/javascript\">\nvar foovar = new Foobar();\nfoovar.addDataSet(", $this->r->render());
|
2013-09-19 16:31:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testJQueryNoConflictAutoDisabling()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->r->isJqueryNoConflictEnabled());
|
|
|
|
$this->r->setIncludeVendors(false);
|
|
|
|
$this->assertFalse($this->r->isJqueryNoConflictEnabled());
|
|
|
|
$this->r->setEnableJqueryNoConflict(true);
|
|
|
|
$this->r->setIncludeVendors('css');
|
|
|
|
$this->assertFalse($this->r->isJqueryNoConflictEnabled());
|
|
|
|
$this->r->setEnableJqueryNoConflict(true);
|
|
|
|
$this->r->setIncludeVendors(array('css', 'js'));
|
|
|
|
$this->assertTrue($this->r->isJqueryNoConflictEnabled());
|
2013-08-14 21:37:24 +10:00
|
|
|
}
|
2014-11-23 09:20:04 +01:00
|
|
|
|
|
|
|
public function testCanDisableSpecificVendors()
|
|
|
|
{
|
2020-12-07 11:39:43 +01:00
|
|
|
$this->assertStringContainsString('jquery.min.js', $this->r->renderHead());
|
2014-11-23 09:20:04 +01:00
|
|
|
$this->r->disableVendor('jquery');
|
2020-12-07 11:39:43 +01:00
|
|
|
$this->assertStringNotContainsString('jquery.min.js', $this->r->renderHead());
|
2014-11-23 09:20:04 +01:00
|
|
|
}
|
2014-01-16 21:41:41 +00:00
|
|
|
}
|