You are here

class ViewportResolverTest in Viewport 8

@coversDefaultClass \Drupal\viewport\ViewportResolver @group viewport

Hierarchy

Expanded class hierarchy of ViewportResolverTest

File

tests/src/Unit/ViewportResolverTest.php, line 12

Namespace

Drupal\Tests\viewport\Unit
View source
class ViewportResolverTest extends UnitTestCase {

  /**
   * The tested ViewportResolver class.
   *
   * @var \Drupal\viewport\ViewportResolver
   */
  protected $viewportResolver;

  /**
   * The mocked path matcher.
   *
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  protected $pathMatcher;

  /**
   * The mocked current path stack.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected $currentPathStack;

  /**
   * The mocked config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Instantiate mocks and stubs.
    $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'));

    // Mock PathMatcher class directly (instead of interface) to try and add
    // some coverage of '<front>'.
    $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);
  }

  /**
   * Tests isPathSelected method solves filters out properly configured paths.
   */
  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.');
  }

  /**
   * Tests isPathSelected method solves paths passed as uppercase.
   */
  public function testIsPathSelectedNormalisesPathString() {
    $this
      ->assertTrue($this->viewportResolver
      ->isPathSelected('/pReSent-PATH'), 'Check for configured path.');
  }

  /**
   * Tests isPathSelected method uses current path if none provided.
   */
  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.');
  }

  /**
   * Tests isPathSelected matches frontpage paths not explicitly configured.
   *
   * Code smell?: This test is not really reliable. As it stands it relies on
   * the "<front>" translation to the current frontpage path being done by the
   * PathMatcher class. The mocks in this test overwrite the getFrontPagePath
   * method to return the one desired, but that wouldn't ensure correct behavior
   * if the current path stack service in Drupal changed to a different class.
   */
  public function testIsPathSelectedDetectsFrontPage() {
    $this
      ->assertTrue($this->viewportResolver
      ->isPathSelected('/frontpage-path'), 'Check non-configured path as front page path.');
  }

  /**
   * Tests generateViewportTagArray() generates the correct array structure.
   */
  public function testGenerateViewportTagArray() {

    // Check the tag array returned is exactly as expected, including the order
    // in which 'content' properties are set, although there's no real need to
    // be so strict.
    $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());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
ViewportResolverTest::$configFactory protected property The mocked config factory.
ViewportResolverTest::$currentPathStack protected property The mocked current path stack.
ViewportResolverTest::$pathMatcher protected property The mocked path matcher.
ViewportResolverTest::$viewportResolver protected property The tested ViewportResolver class.
ViewportResolverTest::setUp protected function Overrides UnitTestCase::setUp
ViewportResolverTest::testGenerateViewportTagArray public function Tests generateViewportTagArray() generates the correct array structure.
ViewportResolverTest::testIsPathSelected public function Tests isPathSelected method solves filters out properly configured paths.
ViewportResolverTest::testIsPathSelectedDetectsFrontPage public function Tests isPathSelected matches frontpage paths not explicitly configured.
ViewportResolverTest::testIsPathSelectedNormalisesPathString public function Tests isPathSelected method solves paths passed as uppercase.
ViewportResolverTest::testIsPathSelectedWithoutPathProvided public function Tests isPathSelected method uses current path if none provided.