BreakpointDiscoveryTest.php 6.7 KB
Newer Older
1 2 3
<?php
/**
 * @file
4
 * Contains \Drupal\breakpoint\Tests\BreakpointDiscoveryTest.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 */

namespace Drupal\breakpoint\Tests;

use Drupal\simpletest\KernelTestBase;

/**
 * Tests discovery of breakpoints provided by themes and modules.
 *
 * @group breakpoint
 */
class BreakpointDiscoveryTest extends KernelTestBase {

  /**
19
   * Modules to install.
20 21 22
   *
   * @var array
   */
23
  public static $modules = array('system', 'breakpoint', 'breakpoint_module_test');
24 25 26

  protected function setUp() {
    parent::setUp();
27
    $this->installSchema('system', array('router'));
28
    \Drupal::service('theme_handler')->install(array('breakpoint_theme_test'));
29 30 31 32 33 34 35 36
  }

  /**
   * Test the breakpoint group created for a theme.
   */
  public function testThemeBreakpoints() {
    // Verify the breakpoint group for breakpoint_theme_test was created.
    $expected_breakpoints = array(
37 38 39
      'breakpoint_theme_test.mobile' => array(
        'label' => 'mobile',
        'mediaQuery' => '(min-width: 0px)',
40 41 42
        'weight' => 0,
        'multipliers' => array(
          '1x',
43
        ),
44
        'provider' => 'breakpoint_theme_test',
45
        'id' => 'breakpoint_theme_test.mobile',
46 47 48
        'group' => 'breakpoint_theme_test',
        'class' => 'Drupal\\breakpoint\\Breakpoint',
      ),
49 50 51
      'breakpoint_theme_test.narrow' => array(
        'label' => 'narrow',
        'mediaQuery' => '(min-width: 560px)',
52 53 54
        'weight' => 1,
        'multipliers' => array(
          '1x',
55
        ),
56
        'provider' => 'breakpoint_theme_test',
57
        'id' => 'breakpoint_theme_test.narrow',
58 59 60
        'group' => 'breakpoint_theme_test',
        'class' => 'Drupal\\breakpoint\\Breakpoint',
      ),
61 62 63
      'breakpoint_theme_test.wide' => array(
        'label' => 'wide',
        'mediaQuery' => '(min-width: 851px)',
64 65 66
        'weight' => 2,
        'multipliers' => array(
          '1x',
67
        ),
68
        'provider' => 'breakpoint_theme_test',
69
        'id' => 'breakpoint_theme_test.wide',
70 71 72
        'group' => 'breakpoint_theme_test',
        'class' => 'Drupal\\breakpoint\\Breakpoint',
      ),
73 74 75
      'breakpoint_theme_test.tv' => array(
        'label' => 'tv',
        'mediaQuery' => 'only screen and (min-width: 1220px)',
76 77 78
        'weight' => 3,
        'multipliers' => array(
          '1x',
79
        ),
80
        'provider' => 'breakpoint_theme_test',
81
        'id' => 'breakpoint_theme_test.tv',
82 83 84
        'group' => 'breakpoint_theme_test',
        'class' => 'Drupal\\breakpoint\\Breakpoint',
      ),
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    );

    $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('breakpoint_theme_test');
    foreach ($expected_breakpoints as $id => $expected_breakpoint) {
      $this->assertEqual($expected_breakpoint, $breakpoints[$id]->getPluginDefinition());
    }

    // Test that the order is as expected.
    $this->assertIdentical(array_keys($expected_breakpoints), array_keys($breakpoints));
  }

  /**
   * Test the custom breakpoint group provided by a theme and a module.
   */
  public function testCustomBreakpointGroups () {
    // Verify the breakpoint group for breakpoint_theme_test.group2 was created.
    $expected_breakpoints = array(
      'breakpoint_theme_test.group2.narrow' => array(
        'label' => 'narrow',
        'mediaQuery' => '(min-width: 560px)',
105
        'weight' => 0,
106 107 108 109 110 111 112 113 114 115 116 117
        'multipliers' => array(
          '1x',
          '2x',
        ),
        'provider' => 'breakpoint_theme_test',
        'id' => 'breakpoint_theme_test.group2.narrow',
        'group' => 'breakpoint_theme_test.group2',
        'class' => 'Drupal\\breakpoint\\Breakpoint',
      ),
      'breakpoint_theme_test.group2.wide' => array(
        'label' => 'wide',
        'mediaQuery' => '(min-width: 851px)',
118
        'weight' => 1,
119 120 121 122 123 124 125 126 127 128 129 130
        'multipliers' => array(
          '1x',
          '2x',
        ),
        'provider' => 'breakpoint_theme_test',
        'id' => 'breakpoint_theme_test.group2.wide',
        'group' => 'breakpoint_theme_test.group2',
        'class' => 'Drupal\\breakpoint\\Breakpoint',
      ),
      'breakpoint_module_test.breakpoint_theme_test.group2.tv' => array(
        'label' => 'tv',
        'mediaQuery' => '(min-width: 6000px)',
131
        'weight' => 2,
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        'multipliers' => array(
          '1x',
        ),
        'provider' => 'breakpoint_module_test',
        'id' => 'breakpoint_module_test.breakpoint_theme_test.group2.tv',
        'group' => 'breakpoint_theme_test.group2',
        'class' => 'Drupal\\breakpoint\\Breakpoint',
      ),
    );

    $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('breakpoint_theme_test.group2');
    foreach ($expected_breakpoints as $id => $expected_breakpoint) {
      $this->assertEqual($expected_breakpoint, $breakpoints[$id]->getPluginDefinition());
    }
  }

  /**
   * Test the breakpoint group created for a module.
   */
  public function testModuleBreakpoints() {
    $expected_breakpoints = array(
      'breakpoint_module_test.mobile' => array(
        'label' => 'mobile',
        'mediaQuery' => '(min-width: 0px)',
156
        'weight' => 0,
157 158 159 160 161 162 163 164 165 166 167
        'multipliers' => array(
          '1x',
        ),
        'provider' => 'breakpoint_module_test',
        'id' => 'breakpoint_module_test.mobile',
        'group' => 'breakpoint_module_test',
        'class' => 'Drupal\\breakpoint\\Breakpoint',
      ),
      'breakpoint_module_test.standard' => array(
        'label' => 'standard',
        'mediaQuery' => '(min-width: 560px)',
168
        'weight' => 1,
169 170 171 172 173 174 175 176 177 178 179 180
        'multipliers' => array(
          '1x',
          '2x',
        ),
        'provider' => 'breakpoint_module_test',
        'id' => 'breakpoint_module_test.standard',
        'group' => 'breakpoint_module_test',
        'class' => 'Drupal\\breakpoint\\Breakpoint',
      ),
    );

    $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('breakpoint_module_test');
181
    $this->assertEqual(array_keys($expected_breakpoints), array_keys($breakpoints));
182 183 184 185 186 187 188 189 190 191 192 193 194 195
  }

  /**
   * Test the collection of breakpoint groups.
   */
  public function testBreakpointGroups() {
    $expected = array(
      'bartik' => 'Bartik',
      'breakpoint_module_test' => 'Breakpoint test module',
      'breakpoint_theme_test' => 'Breakpoint test theme',
      'breakpoint_theme_test.group2' => 'breakpoint_theme_test.group2',
    );
    $breakpoint_groups = \Drupal::service('breakpoint.manager')->getGroups();
    // Ensure the order is as expected. Should be sorted by label.
196
    $this->assertIdentical($expected, $this->castSafeStrings($breakpoint_groups));
197 198 199 200 201 202 203 204 205 206

    $expected = array(
      'breakpoint_theme_test' => 'theme',
      'breakpoint_module_test' => 'module',
    );
    $breakpoint_group_providers = \Drupal::service('breakpoint.manager')->getGroupProviders('breakpoint_theme_test.group2');
    $this->assertEqual($expected, $breakpoint_group_providers);
  }

}