View source
<?php
namespace Drupal\Tests\viewport\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\viewport\ViewportResolver;
class ViewportResolverTest extends UnitTestCase {
protected $viewportResolver;
protected $pathMatcher;
protected $currentPathStack;
protected $configFactory;
protected function setUp() {
parent::setUp();
$configuredPaths = "<front>\n/node/*\n/present-path";
$this->configFactory = $this
->getConfigFactoryStub(array(
'viewport.settings' => array(
'selected_pages' => $configuredPaths,
'width' => '1600',
'height' => 'device-height',
'initial_scale' => '2.0',
'minimum_scale' => '1.0',
'maximum_scale' => '5.0',
'user_scalable' => TRUE,
),
));
$requestStack = $this
->getMock('\\Symfony\\Component\\HttpFoundation\\RequestStack');
$this->currentPathStack = $this
->getMock('\\Drupal\\Core\\Path\\CurrentPathStack', array(), [
$requestStack,
]);
$this->currentPathStack
->expects($this
->any())
->method('getPath')
->will($this
->onConsecutiveCalls('/present-path', '/not-present-path'));
$this->pathMatcher = $this
->getMock('\\Drupal\\Core\\Path\\PathMatcher', array(
'getFrontPagePath',
), array(), '', FALSE);
$this->pathMatcher
->expects($this
->any())
->method('getFrontPagePath')
->will($this
->returnValue('/frontpage-path'));
$this->viewportResolver = new ViewportResolver($this->pathMatcher, $this->currentPathStack, $this->configFactory);
}
public function testIsPathSelected() {
$this
->assertTrue($this->viewportResolver
->isPathSelected('/present-path'), 'Check for configured path.');
$this
->assertFalse($this->viewportResolver
->isPathSelected('/not-present-path'), 'Check for non-configured path.');
}
public function testIsPathSelectedNormalisesPathString() {
$this
->assertTrue($this->viewportResolver
->isPathSelected('/pReSent-PATH'), 'Check for configured path.');
}
public function testIsPathSelectedWithoutPathProvided() {
$this
->assertTrue($this->viewportResolver
->isPathSelected(), 'Check configured path as current path.');
$this
->assertFalse($this->viewportResolver
->isPathSelected(), 'Check non-configured path as current path.');
}
public function testIsPathSelectedDetectsFrontPage() {
$this
->assertTrue($this->viewportResolver
->isPathSelected('/frontpage-path'), 'Check non-configured path as front page path.');
}
public function testGenerateViewportTagArray() {
$expected = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'viewport',
'content' => "width=1600, height=device-height, initial-scale=2.0, minimum-scale=1.0, maximum-scale=5.0, user-scalable=yes",
),
);
$this
->assertArrayEquals($expected, $this->viewportResolver
->generateViewportTagArray());
}
}