diff -u b/core/lib/Drupal/Core/Extension/ThemeHandler.php b/core/lib/Drupal/Core/Extension/ThemeHandler.php --- b/core/lib/Drupal/Core/Extension/ThemeHandler.php +++ b/core/lib/Drupal/Core/Extension/ThemeHandler.php @@ -684,2 +684,14 @@ } + + /** + * {@inheritdoc} + */ + public function getTheme($name) { + $themes = $this->listInfo(); + if (isset($themes[$name])) { + return $themes[$name]; + } + throw new \InvalidArgumentException(sprintf('The theme %s does not exist.', $name)); + } + } diff -u b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php --- b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php +++ b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php @@ -154,2 +154,16 @@ + /** + * Returns a theme extension object from the currently active theme list. + * + * @param string $name + * The name of the theme to return. + * + * @return \Drupal\Core\Extension\Extension + * An extension object. + * + * @throws \InvalidArgumentException + * Thrown when the requested theme does not exist. + */ + public function getTheme($name); + } diff -u b/core/modules/breakpoint/breakpoint.module b/core/modules/breakpoint/breakpoint.module --- b/core/modules/breakpoint/breakpoint.module +++ b/core/modules/breakpoint/breakpoint.module @@ -37,0 +38,14 @@ + +/** + * Implements hook_themes_enabled() + */ +function breakpoint_themes_enabled($theme_list) { + \Drupal::service('breakpoint.manager')->clearCachedDefinitions(); +} + +/** + * Implements hook_themes_disabled() + */ +function breakpoint_themes_disabled($theme_list) { + \Drupal::service('breakpoint.manager')->clearCachedDefinitions(); +} diff -u b/core/modules/breakpoint/src/BreakpointManager.php b/core/modules/breakpoint/src/BreakpointManager.php --- b/core/modules/breakpoint/src/BreakpointManager.php +++ b/core/modules/breakpoint/src/BreakpointManager.php @@ -7,6 +7,7 @@ namespace Drupal\breakpoint; +use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ThemeHandlerInterface; @@ -58,7 +59,7 @@ protected $breakpointsByGroup; /** - * Constructs a new ContextualLinkManager instance. + * Constructs a new BreakpointManager instance. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. @@ -126,23 +127,23 @@ /** * {@inheritdoc} */ - public function getBreakpointsByGroup($group_name) { - if (!isset($this->breakpointsByGroup[$group_name])) { - if ($cache = $this->cacheBackend->get($this->cacheKey . ':' . $group_name)) { - $this->breakpointsByGroup[$group_name] = $cache->data; + public function getBreakpointsByGroup($group) { + if (!isset($this->breakpointsByGroup[$group])) { + if ($cache = $this->cacheBackend->get($this->cacheKey . ':' . $group)) { + $this->breakpointsByGroup[$group] = $cache->data; } else { $breakpoints = array(); foreach ($this->getDefinitions() as $plugin_id => $plugin_definition) { - if ($plugin_definition['group'] == $group_name) { + if ($plugin_definition['group'] == $group) { $breakpoints[$plugin_id] = $plugin_definition; } } - $this->cacheBackend->set($this->cacheKey . ':' . $group_name, $breakpoints); - $this->breakpointsByGroup[$group_name] = $breakpoints; + $this->cacheBackend->set($this->cacheKey . ':' . $group, $breakpoints, Cache::PERMANENT, array('breakpoints' => TRUE)); + $this->breakpointsByGroup[$group] = $breakpoints; } } - return $this->breakpointsByGroup[$group_name]; + return $this->breakpointsByGroup[$group]; } /** @@ -160,11 +161,41 @@ } } asort($groups); - $this->cacheBackend->set($this->cacheKey . '::groups', $groups); + $this->cacheBackend->set($this->cacheKey . '::groups', $groups, Cache::PERMANENT, array('breakpoints' => TRUE)); return $groups; } /** + * {@inheritdoc} + */ + public function getGroupProviders($group) { + $providers = array(); + $breakpoints = $this->getBreakpointsByGroup($group); + foreach ($breakpoints as $breakpoint) { + $provider = $breakpoint['provider']; + $extension = FALSE; + if ($this->moduleHandler->moduleExists($provider)) { + $extension = $this->moduleHandler->getModule($provider); + } + elseif ($this->themeHandler->themeExists($provider)) { + $extension = $this->themeHandler->getTheme($provider); + } + if ($extension) { + $providers[$extension->getName()] = $extension->getType(); + } + } + return $providers; + } + + /** + * {@inheritdoc} + */ + public function clearCachedDefinitions() { + parent::clearCachedDefinitions(); + $this->breakpointsByGroup = NULL; + } + + /** * @param string $group * * @return string diff -u b/core/modules/breakpoint/src/BreakpointManagerInterface.php b/core/modules/breakpoint/src/BreakpointManagerInterface.php --- b/core/modules/breakpoint/src/BreakpointManagerInterface.php +++ b/core/modules/breakpoint/src/BreakpointManagerInterface.php @@ -15,13 +15,13 @@ /** * Gets breakpoints for the specified group. * - * @param string $group_name + * @param string $group * The breakpoint group to retrieve. * * @return array * Array of breakpoints keyed by machine name. */ - public function getBreakpointsByGroup($group_name); + public function getBreakpointsByGroup($group); /** * Gets all the existing breakpoint groups. @@ -34 +34,13 @@ -} \ No newline at end of file + /** + * Gets all the providers for the specified breakpoint group. + * + * @param string $group + * The breakpoint group to retrieve. + * + * @return array + * An array keyed by provider name with values of provider type (module or + * theme). + */ + public function getGroupProviders($group); + +} diff -u b/core/modules/breakpoint/src/Tests/BreakpointDiscoveryTest.php b/core/modules/breakpoint/src/Tests/BreakpointDiscoveryTest.php --- b/core/modules/breakpoint/src/Tests/BreakpointDiscoveryTest.php +++ b/core/modules/breakpoint/src/Tests/BreakpointDiscoveryTest.php @@ -20,99 +20,115 @@ * * @var array */ - public static $modules = array('breakpoint', 'breakpoints_module_test'); + public static $modules = array('breakpoint', 'breakpoint_module_test'); public function setUp() { parent::setUp(); - \Drupal::service('theme_handler')->enable(array('breakpoint_test_theme')); + \Drupal::service('theme_handler')->enable(array('breakpoint_theme_test')); } /** * Test the breakpoint group created for a theme. */ public function testThemeBreakpoints() { - // Verify the breakpoint group for breakpoint_test_theme was created. + // Verify the breakpoint group for breakpoint_theme_test was created. $expected_breakpoints = array( - 'breakpoint_test_theme.mobile' => array( + 'breakpoint_theme_test.mobile' => array( 'label' => 'mobile', 'mediaQuery' => '(min-width: 0px)', 'weight' => 0, 'multipliers' => array( '1x', ), - 'provider' => 'breakpoint_test_theme', - 'id' => 'breakpoint_test_theme.mobile', - 'group' => 'breakpoint_test_theme', + 'provider' => 'breakpoint_theme_test', + 'id' => 'breakpoint_theme_test.mobile', + 'group' => 'breakpoint_theme_test', ), - 'breakpoint_test_theme.narrow' => array( + 'breakpoint_theme_test.narrow' => array( 'label' => 'narrow', 'mediaQuery' => '(min-width: 560px)', 'weight' => 1, 'multipliers' => array( '1x', ), - 'provider' => 'breakpoint_test_theme', - 'id' => 'breakpoint_test_theme.narrow', - 'group' => 'breakpoint_test_theme', + 'provider' => 'breakpoint_theme_test', + 'id' => 'breakpoint_theme_test.narrow', + 'group' => 'breakpoint_theme_test', ), - 'breakpoint_test_theme.wide' => array( + 'breakpoint_theme_test.wide' => array( 'label' => 'wide', 'mediaQuery' => '(min-width: 851px)', 'weight' => 2, 'multipliers' => array( '1x', ), - 'provider' => 'breakpoint_test_theme', - 'id' => 'breakpoint_test_theme.wide', - 'group' => 'breakpoint_test_theme', + 'provider' => 'breakpoint_theme_test', + 'id' => 'breakpoint_theme_test.wide', + 'group' => 'breakpoint_theme_test', ), - 'breakpoint_test_theme.tv' => array( + 'breakpoint_theme_test.tv' => array( 'label' => 'tv', 'mediaQuery' => 'only screen and (min-width: 3456px)', 'weight' => 3, 'multipliers' => array( '1x', ), - 'provider' => 'breakpoint_test_theme', - 'id' => 'breakpoint_test_theme.tv', - 'group' => 'breakpoint_test_theme', + 'provider' => 'breakpoint_theme_test', + 'id' => 'breakpoint_theme_test.tv', + 'group' => 'breakpoint_theme_test', ), ); - $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('breakpoint_test_theme'); + $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('breakpoint_theme_test'); foreach ($expected_breakpoints as $id => $expected_breakpoint) { $this->assertEqual($expected_breakpoint, $breakpoints[$id]); } + } - // Verify the breakpoint group for breakpoint_test_theme.group2 was created. + /** + * 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_test_theme.group2.narrow' => array( + 'breakpoint_theme_test.group2.narrow' => array( 'label' => 'narrow', 'mediaQuery' => '(min-width: 560px)', 'weight' => 1, 'multipliers' => array( '1x', - '2x' + '2x', ), - 'provider' => 'breakpoint_test_theme', - 'id' => 'breakpoint_test_theme.group2.narrow', - 'group' => 'breakpoint_test_theme.group2', + 'provider' => 'breakpoint_theme_test', + 'id' => 'breakpoint_theme_test.group2.narrow', + 'group' => 'breakpoint_theme_test.group2', ), - 'breakpoint_test_theme.group2.wide' => array( + 'breakpoint_theme_test.group2.wide' => array( 'label' => 'wide', 'mediaQuery' => '(min-width: 851px)', 'weight' => 2, 'multipliers' => array( '1x', - '2x' + '2x', + ), + 'provider' => 'breakpoint_theme_test', + 'id' => 'breakpoint_theme_test.group2.wide', + 'group' => 'breakpoint_theme_test.group2', + ), + 'breakpoint_module_test.breakpoint_theme_test.group2.tv' => array( + 'label' => 'tv', + 'mediaQuery' => '(min-width: 6000px)', + 'weight' => 3, + 'multipliers' => array( + '1x', ), - 'provider' => 'breakpoint_test_theme', - 'id' => 'breakpoint_test_theme.group2.wide', - 'group' => 'breakpoint_test_theme.group2', + 'provider' => 'breakpoint_module_test', + 'id' => 'breakpoint_module_test.breakpoint_theme_test.group2.tv', + 'group' => 'breakpoint_theme_test.group2', ), ); - $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('breakpoint_test_theme.group2'); + $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('breakpoint_theme_test.group2'); foreach ($expected_breakpoints as $id => $expected_breakpoint) { $this->assertEqual($expected_breakpoint, $breakpoints[$id]); } @@ -123,18 +139,18 @@ */ public function testModuleBreakpoints() { $expected_breakpoints = array( - 'breakpoints_module_test.mobile' => array( + 'breakpoint_module_test.mobile' => array( 'label' => 'mobile', 'mediaQuery' => '(min-width: 0px)', 'weight' => 0, 'multipliers' => array( '1x', ), - 'provider' => 'breakpoints_module_test', - 'id' => 'breakpoints_module_test.mobile', - 'group' => 'breakpoints_module_test', + 'provider' => 'breakpoint_module_test', + 'id' => 'breakpoint_module_test.mobile', + 'group' => 'breakpoint_module_test', ), - 'breakpoints_module_test.standard' => array( + 'breakpoint_module_test.standard' => array( 'label' => 'standard', 'mediaQuery' => '(min-width: 560px)', 'weight' => 1, @@ -142,13 +158,13 @@ '1x', '2x', ), - 'provider' => 'breakpoints_module_test', - 'id' => 'breakpoints_module_test.standard', - 'group' => 'breakpoints_module_test', + 'provider' => 'breakpoint_module_test', + 'id' => 'breakpoint_module_test.standard', + 'group' => 'breakpoint_module_test', ), ); - $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('breakpoints_module_test'); + $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup('breakpoint_module_test'); foreach ($expected_breakpoints as $id => $expected_breakpoint) { $this->assertEqual($expected_breakpoint, $breakpoints[$id]); } @@ -160,12 +176,19 @@ public function testBreakpointGroups() { $expected = array( 'bartik' => 'Bartik', - 'breakpoints_module_test' => 'Breakpoint test module', - 'breakpoint_test_theme' => 'Breakpoint test theme', - 'breakpoint_test_theme.group2' => 'breakpoint_test_theme.group2', + '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(); $this->assertEqual($expected, $breakpoint_groups); + + $expected = array( + 'breakpoint_module_test' => 'module', + 'breakpoint_theme_test' => 'theme', + ); + $breakpoint_group_providers = \Drupal::service('breakpoint.manager')->getGroupProviders('breakpoint_theme_test.group2'); + $this->assertEqual($expected, $breakpoint_group_providers); } } reverted: --- b/core/modules/breakpoint/tests/modules/breakpoints_module_test/breakpoints_module_test.breakpoints.yml +++ /dev/null @@ -1,12 +0,0 @@ -breakpoints_module_test.mobile: - label: mobile - mediaQuery: '(min-width: 0px)' - weight: 0 - # Don't include multipliers. A 1x multiplier this will be enforced by default. -breakpoints_module_test.standard: - label: standard - mediaQuery: '(min-width: 560px)' - weight: 1 - # Don't include a 1x multiplier this will be enforced by default. - multipliers: - - 2x reverted: --- b/core/modules/breakpoint/tests/modules/breakpoints_module_test/breakpoints_module_test.info.yml +++ /dev/null @@ -1,6 +0,0 @@ -name: 'Breakpoint test module' -type: module -description: 'Test module for breakpoint.' -version: VERSION -core: 8.x - reverted: --- b/core/modules/breakpoint/tests/themes/breakpoint_test_theme/breakpoint_test_theme.breakpoints.yml +++ /dev/null @@ -1,40 +0,0 @@ -breakpoint_test_theme.mobile: - label: mobile - mediaQuery: '(min-width: 0px)' - weight: 0 - multipliers: - - 1x -breakpoint_test_theme.narrow: - label: narrow - mediaQuery: '(min-width: 560px)' - weight: 1 - multipliers: - - 1x -breakpoint_test_theme.wide: - label: wide - mediaQuery: '(min-width: 851px)' - weight: 2 - multipliers: - - 1x -breakpoint_test_theme.tv: - label: tv - mediaQuery: 'only screen and (min-width: 3456px)' - weight: 3 - multipliers: - - 1x -breakpoint_test_theme.group2.narrow: - label: narrow - mediaQuery: '(min-width: 560px)' - weight: 1 - multipliers: - - 1x - - 2x - group: breakpoint_test_theme.group2 -breakpoint_test_theme.group2.wide: - label: wide - mediaQuery: '(min-width: 851px)' - weight: 2 - multipliers: - - 1x - - 2x - group: breakpoint_test_theme.group2 diff -u b/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php b/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php --- b/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php +++ b/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php @@ -81,6 +81,13 @@ /** * {@inheritdoc} */ + public function __construct(array $values, $entity_type_id = 'responsive_image_mapping') { + parent::__construct($values, $entity_type_id); + } + + /** + * {@inheritdoc} + */ public function addMapping($breakpoint_id, $multiplier, $image_style) { foreach ($this->mappings as &$mapping) { if ($mapping['breakpoint_id'] === $breakpoint_id && $mapping['multiplier'] === $multiplier) { @@ -93,6 +100,7 @@ 'multiplier' => $multiplier, 'image_style' => $image_style, ); + $this->keyedMappings = NULL; return $this; } @@ -109,14 +117,8 @@ public function getKeyedMappings() { if (!$this->keyedMappings) { $this->keyedMappings = array(); - $valid_breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup($this->breakpointGroup); foreach($this->mappings as $mapping) { - // If the breakpoint exists and the multiplier exists. - // @todo think about if this is just super defensive? - if (isset($valid_breakpoints[$mapping['breakpoint_id']]) - && in_array($mapping['multiplier'], $valid_breakpoints[$mapping['breakpoint_id']]['multipliers'])) { - $this->keyedMappings[$mapping['breakpoint_id']][$mapping['multiplier']] = $mapping['image_style']; - } + $this->keyedMappings[$mapping['breakpoint_id']][$mapping['multiplier']] = $mapping['image_style']; } } return $this->keyedMappings; @@ -143,6 +145,10 @@ * {@inheritdoc} */ public function setBreakpointGroup($breakpoint_group) { + // If the breakpoint group is changed then the mappings are invalid. + if ($breakpoint_group !== $this->breakpointGroup) { + $this->removeMappings(); + } $this->set('breakpointGroup', $breakpoint_group); return $this; } @@ -161,5 +167,18 @@ $this->mappings = array(); + $this->keyedMappings = NULL; return $this; } + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + parent::calculateDependencies(); + $providers = \Drupal::service('breakpoint.manager')->getGroupProviders($this->breakpointGroup); + foreach ($providers as $provider => $type) { + $this->addDependency($type, $provider); + } + return $this->dependencies; + } + } diff -u b/core/themes/seven/seven.breakpoints.yml b/core/themes/seven/seven.breakpoints.yml --- b/core/themes/seven/seven.breakpoints.yml +++ b/core/themes/seven/seven.breakpoints.yml @@ -12 +12 @@ - - 1x \ No newline at end of file + - 1x reverted: --- b/core/themes/stark/stark.info.yml +++ a/core/themes/stark/stark.info.yml @@ -9,26 +9,3 @@ - css/layout.css stylesheets-remove: - normalize.css - -breakpoints: - mobile: - name: mobile - label: mobile - mediaQuery: '(min-width: 0px)' - weight: 0 - multipliers: - - 1x - narrow: - name: narrow - label: narrow - mediaQuery: 'all and (min-width: 480px) and (max-width: 959px)' - weight: 1 - multipliers: - - 1x - wide: - name: wide - label: wide - mediaQuery: 'all and (min-width: 960px)' - weight: 2 - multipliers: - - 1x only in patch2: unchanged: --- /dev/null +++ b/core/modules/breakpoint/tests/modules/breakpoint_module_test/breakpoint_module_test.breakpoints.yml @@ -0,0 +1,21 @@ +breakpoint_module_test.mobile: + label: mobile + mediaQuery: '(min-width: 0px)' + weight: 0 + # Don't include multipliers. A 1x multiplier this will be enforced by default. +breakpoint_module_test.standard: + label: standard + mediaQuery: '(min-width: 560px)' + weight: 1 + # Don't include a 1x multiplier this will be enforced by default. + multipliers: + - 2x +# Test providing a breakpoint for group matching the group provided by +# breakpoint_test_theme. +breakpoint_module_test.breakpoint_theme_test.group2.tv: + label: tv + mediaQuery: '(min-width: 6000px)' + weight: 3 + multipliers: + - 1x + group: breakpoint_theme_test.group2 only in patch2: unchanged: --- /dev/null +++ b/core/modules/breakpoint/tests/modules/breakpoint_module_test/breakpoint_module_test.info.yml @@ -0,0 +1,6 @@ +name: 'Breakpoint test module' +type: module +description: 'Test module for breakpoint.' +version: VERSION +core: 8.x + only in patch2: unchanged: --- /dev/null +++ b/core/modules/breakpoint/tests/themes/breakpoint_theme_test/breakpoint_theme_test.breakpoints.yml @@ -0,0 +1,40 @@ +breakpoint_theme_test.mobile: + label: mobile + mediaQuery: '(min-width: 0px)' + weight: 0 + multipliers: + - 1x +breakpoint_theme_test.narrow: + label: narrow + mediaQuery: '(min-width: 560px)' + weight: 1 + multipliers: + - 1x +breakpoint_theme_test.wide: + label: wide + mediaQuery: '(min-width: 851px)' + weight: 2 + multipliers: + - 1x +breakpoint_theme_test.tv: + label: tv + mediaQuery: 'only screen and (min-width: 3456px)' + weight: 3 + multipliers: + - 1x +breakpoint_theme_test.group2.narrow: + label: narrow + mediaQuery: '(min-width: 560px)' + weight: 1 + multipliers: + - 1x + - 2x + group: breakpoint_theme_test.group2 +breakpoint_theme_test.group2.wide: + label: wide + mediaQuery: '(min-width: 851px)' + weight: 2 + multipliers: + - 1x + - 2x + group: breakpoint_theme_test.group2 only in patch2: unchanged: --- /dev/null +++ b/core/modules/responsive_image/tests/src/ResponsiveImageMappingConfigEntityUnitTest.php @@ -0,0 +1,198 @@ +entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); + $this->entityType->expects($this->any()) + ->method('getProvider') + ->will($this->returnValue('responsive_image')); + + $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); + $this->entityManager->expects($this->any()) + ->method('getDefinition') + ->with('responsive_image_mapping') + ->will($this->returnValue($this->entityType)); + + $this->breakpointManager = $this->getMock('\Drupal\breakpoint\BreakpointManagerInterface'); + + $container = new ContainerBuilder(); + $container->set('entity.manager', $this->entityManager); + $container->set('breakpoint.manager', $this->breakpointManager); + \Drupal::setContainer($container); + } + + /** + * @covers ::calculateDependencies + */ + public function testCalculateDependencies() { + $entity = new ResponsiveImageMapping(array('breakpointGroup' => 'test_group')); + $entity->setBreakpointGroup('test_group'); + + $this->breakpointManager->expects($this->any()) + ->method('getGroupProviders') + ->with('test_group') + ->willReturn(array('bartik' => 'theme', 'toolbar' => 'module')); + + $dependencies = $entity->calculateDependencies(); + $this->assertContains('toolbar', $dependencies['module']); + $this->assertContains('bartik', $dependencies['theme']); + } + + /** + * @covers ::addMapping + * @covers ::hasMapping + */ + public function testHasMappings() { + $entity = new ResponsiveImageMapping(array()); + $this->assertFalse($entity->hasMappings()); + $entity->addMapping('test_breakpoint', '1x', 'test_style'); + $this->assertTrue($entity->hasMappings()); + } + + /** + * @covers ::addMapping + * @covers ::getImageStyle + */ + public function testGetImageStyle() { + $entity = new ResponsiveImageMapping(array('')); + $entity->addMapping('test_breakpoint', '1x', 'test_style'); + $this->assertEquals('test_style', $entity->getImageStyle('test_breakpoint', '1x')); + $this->assertNull($entity->getImageStyle('test_unknown_breakpoint', '1x')); + } + + /** + * @covers ::addMapping + * @covers ::getMappings + */ + public function testGetKeyedMappings() { + $entity = new ResponsiveImageMapping(array('')); + $entity->addMapping('test_breakpoint', '1x', 'test_style'); + $entity->addMapping('test_breakpoint', '2x', 'test_style2'); + $entity->addMapping('test_breakpoint2', '1x', 'test_style3'); + + $expected = array( + 'test_breakpoint' => array( + '1x' => 'test_style', + '2x' => 'test_style2', + ), + 'test_breakpoint2' => array( + '1x' => 'test_style3', + ) + ); + $this->assertEquals($expected, $entity->getKeyedMappings()); + + // Add another mapping to ensure keyed mapping static cache is rebuilt. + $entity->addMapping('test_breakpoint2', '2x', 'test_style4'); + $expected['test_breakpoint2']['2x'] = 'test_style4'; + $this->assertEquals($expected, $entity->getKeyedMappings()); + } + + /** + * @covers ::addMapping + * @covers ::getMappings + */ + public function testGetMappings() { + $entity = new ResponsiveImageMapping(array('')); + $entity->addMapping('test_breakpoint', '1x', 'test_style'); + $entity->addMapping('test_breakpoint', '2x', 'test_style2'); + $entity->addMapping('test_breakpoint2', '1x', 'test_style3'); + + $expected = array( + array( + 'breakpoint_id' => 'test_breakpoint', + 'multiplier' => '1x', + 'image_style' => 'test_style', + ), + array( + 'breakpoint_id' => 'test_breakpoint', + 'multiplier' => '2x', + 'image_style' => 'test_style2', + ), + array( + 'breakpoint_id' => 'test_breakpoint2', + 'multiplier' => '1x', + 'image_style' => 'test_style3', + ), + ); + $this->assertEquals($expected, $entity->getMappings()); + } + + /** + * @covers ::addMapping + * @covers ::removeMappings + */ + public function testRemoveMappings() { + $entity = new ResponsiveImageMapping(array('')); + $entity->addMapping('test_breakpoint', '1x', 'test_style'); + $entity->addMapping('test_breakpoint', '2x', 'test_style2'); + $entity->addMapping('test_breakpoint2', '1x', 'test_style3'); + + $this->assertTrue($entity->hasMappings()); + $entity->removeMappings(); + $this->assertEmpty($entity->getMappings()); + $this->assertEmpty($entity->getKeyedMappings()); + $this->assertFalse($entity->hasMappings()); + } + + /** + * @covers ::setBreakpointGroup + * @covers ::getBreakpointGroup + */ + public function testSetBreakpointGroup() { + $entity = new ResponsiveImageMapping(array('breakpointGroup' => 'test_group')); + $entity->addMapping('test_breakpoint', '1x', 'test_style'); + $entity->addMapping('test_breakpoint', '2x', 'test_style2'); + $entity->addMapping('test_breakpoint2', '1x', 'test_style3'); + + // Ensure that setting to same group does not remove mappings. + $entity->setBreakpointGroup('test_group'); + $this->assertTrue($entity->hasMappings()); + $this->assertEquals('test_group', $entity->getBreakpointGroup()); + + // Ensure that changing the group removes mappings. + $entity->setBreakpointGroup('test_group2'); + $this->assertEquals('test_group2', $entity->getBreakpointGroup()); + $this->assertFalse($entity->hasMappings()); + } + +} only in patch2: unchanged: --- /dev/null +++ b/core/themes/stark/stark.breakpoints.yml @@ -0,0 +1,18 @@ +stark.mobile: + label: mobile + mediaQuery: '(min-width: 0px)' + weight: 0 + multipliers: + - 1x +stark.narrow: + label: narrow + mediaQuery: 'all and (min-width: 480px) and (max-width: 959px)' + weight: 1 + multipliers: + - 1x +stark.wide: + label: wide + mediaQuery: 'all and (min-width: 960px)' + weight: 2 + multipliers: + - 1x \ No newline at end of file