summaryrefslogtreecommitdiffstats
path: root/core/modules/breakpoint/tests/src/BreakpointMediaQueryTest.php
blob: c8ab81dfbfed3abda8ee484137c4989b6cc7342f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
 * @file
 * Definition of Drupal\breakpoint\Tests\BreakpointMediaQueryTest.
 */

namespace Drupal\breakpoint\Tests;

use Drupal\Tests\UnitTestCase;
use Drupal\breakpoint\Entity\Breakpoint;
use Drupal\breakpoint\InvalidBreakpointMediaQueryException;

/**
 * Tests validation of media queries.
 *
 * @group breakpoint
 */
class BreakpointMediaQueryTest extends UnitTestCase {

  /**
   * Test valid media queries.
   */
  public function testValidMediaQueries() {
    $media_queries = array(
      // Bartik breakpoints.
      '(min-width: 0px)',
      'all and (min-width: 560px) and (max-width:850px)',
      'all and (min-width: 851px)',
      // Seven breakpoints.
      '(min-width: 0em)',
      'screen and (min-width: 40em)',
      // Stark breakpoints.
      '(min-width: 0px)',
      'all and (min-width: 480px) and (max-width: 959px)',
      'all and (min-width: 960px)',
      // Other media queries.
      '(orientation)',
      'all and (orientation)',
      'not all and (orientation)',
      'only all and (orientation)',
      'screen and (width)',
      'screen and (width: 0)',
      'screen and (width: 0px)',
      'screen and (width: 0em)',
      'screen and (min-width: -0)',
      'screen and (max-width: 0)',
      'screen and (max-width: 0.3)',
      'screen and (min-width)',
      // Multiline and comments.
      'screen and /* this is a comment */ (min-width)',
      "screen\nand /* this is a comment */ (min-width)",
      "screen\n\nand /* this is\n a comment */ (min-width)",
      // Unrecognized features are allowed.
      'screen and (-webkit-min-device-pixel-ratio: 7)',
      'screen and (min-orientation: landscape)',
      'screen and (max-orientation: landscape)',
    );

    foreach ($media_queries as $media_query) {
      $this->assertTrue(Breakpoint::isValidMediaQuery($media_query), $media_query . ' is valid.');
    }
  }

  /**
   * Test invalid media queries.
   */
  public function testInvalidMediaQueries() {
    $media_queries = array(
      '',
      'not (orientation)',
      'only (orientation)',
      'all and not all',
      'screen and (width: 0xx)',
      'screen and (width: -8xx)',
      'screen and (width: -xx)',
      'screen and (width: xx)',
      'screen and (width: px)',
      'screen and (width: -8px)',
      'screen and (width: -0.8px)',
      'screen and (height: 0xx)',
      'screen and (height: -8xx)',
      'screen and (height: -xx)',
      'screen and (height: xx)',
      'screen and (height: px)',
      'screen and (height: -8px)',
      'screen and (height: -0.8px)',
      'screen and (device-width: 0xx)',
      'screen and (device-width: -8xx)',
      'screen and (device-width: -xx)',
      'screen and (device-width: xx)',
      'screen and (device-width: px)',
      'screen and (device-width: -8px)',
      'screen and (device-width: -0.8px)',
      'screen and (device-height: 0xx)',
      'screen and (device-height: -8xx)',
      'screen and (device-height: -xx)',
      'screen and (device-height: xx)',
      'screen and (device-height: px)',
      'screen and (device-height: -8px)',
      'screen and (device-height: -0.8px)',
      'screen and (min-orientation)',
      'screen and (max-orientation)',
      'screen and (orientation: bogus)',
      '(orientation: bogus)',
      'screen and (ori"entation: bogus)',
    );

    foreach ($media_queries as $media_query) {
      try {
        $this->assertFalse(Breakpoint::isValidMediaQuery($media_query), $media_query . ' is not valid.');
      }
      catch (InvalidBreakpointMediaQueryException $e) {
        $this->assertTrue(TRUE, sprintf('%s is not valid.', $media_query));
      }
    }
  }
}