CssCollectionGrouperUnitTest.php 4.71 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

namespace Drupal\Tests\Core\Asset;

use Drupal\Core\Asset\CssCollectionGrouper;
use Drupal\Tests\UnitTestCase;

/**
9
 * Tests the CSS asset collection grouper.
10 11 12 13 14 15 16 17
 *
 * @group Asset
 */
class CssCollectionGrouperUnitTest extends UnitTestCase {

  /**
   * A CSS asset grouper.
   *
18
   * @var \Drupal\Core\Asset\CssCollectionGrouper
19 20 21
   */
  protected $grouper;

22
  protected function setUp() {
23 24 25 26 27 28 29 30
    parent::setUp();

    $this->grouper = new CssCollectionGrouper();
  }

  /**
   * Tests \Drupal\Core\Asset\CssCollectionGrouper.
   */
31
  public function testGrouper() {
32 33
    $css_assets = [
      'system.base.css' => [
34 35 36 37 38 39
        'group' => -100,
        'type' => 'file',
        'weight' => 0.012,
        'media' => 'all',
        'preprocess' => TRUE,
        'data' => 'core/modules/system/system.base.css',
40
        'browsers' => ['IE' => TRUE, '!IE' => TRUE],
41
        'basename' => 'system.base.css',
42 43
      ],
      'js.module.css' => [
44 45 46 47 48
        'group' => -100,
        'type' => 'file',
        'weight' => 0.013,
        'media' => 'all',
        'preprocess' => TRUE,
49
        'data' => 'core/modules/system/js.module.css',
50
        'browsers' => ['IE' => TRUE, '!IE' => TRUE],
51
        'basename' => 'js.module.css',
52 53
      ],
      'jquery.ui.core.css' => [
54 55 56 57 58 59
        'group' => -100,
        'type' => 'file',
        'weight' => 0.004,
        'media' => 'all',
        'preprocess' => TRUE,
        'data' => 'core/misc/ui/themes/base/jquery.ui.core.css',
60
        'browsers' => ['IE' => TRUE, '!IE' => TRUE],
61
        'basename' => 'jquery.ui.core.css',
62 63
      ],
      'field.css' => [
64 65 66 67 68 69
        'group' => 0,
        'type' => 'file',
        'weight' => 0.011,
        'media' => 'all',
        'preprocess' => TRUE,
        'data' => 'core/modules/field/theme/field.css',
70
        'browsers' => ['IE' => TRUE, '!IE' => TRUE],
71
        'basename' => 'field.css',
72 73
      ],
      'external.css' => [
74 75 76 77 78 79
        'group' => 0,
        'type' => 'external',
        'weight' => 0.009,
        'media' => 'all',
        'preprocess' => TRUE,
        'data' => 'http://example.com/external.css',
80
        'browsers' => ['IE' => TRUE, '!IE' => TRUE],
81
        'basename' => 'external.css',
82 83
      ],
      'elements.css' => [
84 85 86 87 88
        'group' => 100,
        'media' => 'all',
        'type' => 'file',
        'weight' => 0.001,
        'preprocess' => TRUE,
89
        'data' => 'core/themes/bartik/css/base/elements.css',
90
        'browsers' => ['IE' => TRUE, '!IE' => TRUE],
91
        'basename' => 'elements.css',
92 93
      ],
      'print.css' => [
94 95 96 97 98 99
        'group' => 100,
        'media' => 'print',
        'type' => 'file',
        'weight' => 0.003,
        'preprocess' => TRUE,
        'data' => 'core/themes/bartik/css/print.css',
100
        'browsers' => ['IE' => TRUE, '!IE' => TRUE],
101
        'basename' => 'print.css',
102 103
      ],
    ];
104 105 106

    $groups = $this->grouper->group($css_assets);

107
    $this->assertSame(5, count($groups), "5 groups created.");
108 109

    // Check group 1.
110
    $group = $groups[0];
111 112 113 114 115
    $this->assertSame(-100, $group['group']);
    $this->assertSame('file', $group['type']);
    $this->assertSame('all', $group['media']);
    $this->assertSame(TRUE, $group['preprocess']);
    $this->assertSame(3, count($group['items']));
116
    $this->assertContains($css_assets['system.base.css'], $group['items']);
117
    $this->assertContains($css_assets['js.module.css'], $group['items']);
118 119

    // Check group 2.
120
    $group = $groups[1];
121 122 123 124 125
    $this->assertSame(0, $group['group']);
    $this->assertSame('file', $group['type']);
    $this->assertSame('all', $group['media']);
    $this->assertSame(TRUE, $group['preprocess']);
    $this->assertSame(1, count($group['items']));
126
    $this->assertContains($css_assets['field.css'], $group['items']);
127 128

    // Check group 3.
129
    $group = $groups[2];
130 131 132 133 134
    $this->assertSame(0, $group['group']);
    $this->assertSame('external', $group['type']);
    $this->assertSame('all', $group['media']);
    $this->assertSame(TRUE, $group['preprocess']);
    $this->assertSame(1, count($group['items']));
135
    $this->assertContains($css_assets['external.css'], $group['items']);
136 137

    // Check group 4.
138
    $group = $groups[3];
139 140 141 142 143
    $this->assertSame(100, $group['group']);
    $this->assertSame('file', $group['type']);
    $this->assertSame('all', $group['media']);
    $this->assertSame(TRUE, $group['preprocess']);
    $this->assertSame(1, count($group['items']));
144
    $this->assertContains($css_assets['elements.css'], $group['items']);
145 146

    // Check group 5.
147
    $group = $groups[4];
148 149 150 151 152
    $this->assertSame(100, $group['group']);
    $this->assertSame('file', $group['type']);
    $this->assertSame('print', $group['media']);
    $this->assertSame(TRUE, $group['preprocess']);
    $this->assertSame(1, count($group['items']));
153
    $this->assertContains($css_assets['print.css'], $group['items']);
154 155 156
  }

}