class SectionTest
@coversDefaultClass \Drupal\layout_builder\Section
      
    
@group layout_builder
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase- class \Drupal\Tests\layout_builder\Unit\SectionTest extends \Drupal\Tests\UnitTestCase
 
Expanded class hierarchy of SectionTest
File
- 
              core/modules/ layout_builder/ tests/ src/ Unit/ SectionTest.php, line 17 
Namespace
Drupal\Tests\layout_builder\UnitView source
class SectionTest extends UnitTestCase {
  
  /**
   * The section object to test.
   *
   * @var \Drupal\layout_builder\Section
   */
  protected $section;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->section = new Section('layout_onecol', [], [
      new SectionComponent('existing-uuid', 'some-region', [
        'id' => 'existing-block-id',
      ]),
      (new SectionComponent('second-uuid', 'ordered-region', [
        'id' => 'second-block-id',
      ]))->setWeight(3),
      (new SectionComponent('first-uuid', 'ordered-region', [
        'id' => 'first-block-id',
      ]))->setWeight(2),
    ], [
      'bad_judgement' => [
        'blink_speed' => 'fast',
        'spin_direction' => 'clockwise',
      ],
      'hunt_and_peck' => [
        'delay' => '300ms',
      ],
    ]);
  }
  
  /**
   * @covers ::__construct
   * @covers ::setComponent
   * @covers ::getComponents
   */
  public function testGetComponents() {
    $expected = [
      'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', [
        'id' => 'existing-block-id',
      ]))->setWeight(0),
      'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', [
        'id' => 'second-block-id',
      ]))->setWeight(3),
      'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', [
        'id' => 'first-block-id',
      ]))->setWeight(2),
    ];
    $this->assertComponents($expected, $this->section);
  }
  
  /**
   * @covers ::getComponent
   */
  public function testGetComponentInvalidUuid() {
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('Invalid UUID "invalid-uuid"');
    $this->section
      ->getComponent('invalid-uuid');
  }
  
  /**
   * @covers ::getComponent
   */
  public function testGetComponent() {
    $expected = new SectionComponent('existing-uuid', 'some-region', [
      'id' => 'existing-block-id',
    ]);
    $this->assertEquals($expected, $this->section
      ->getComponent('existing-uuid'));
  }
  
  /**
   * @covers ::removeComponent
   * @covers ::getComponentsByRegion
   */
  public function testRemoveComponent() {
    $expected = [
      'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', [
        'id' => 'existing-block-id',
      ]))->setWeight(0),
      'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', [
        'id' => 'second-block-id',
      ]))->setWeight(3),
    ];
    $this->section
      ->removeComponent('first-uuid');
    $this->assertComponents($expected, $this->section);
  }
  
  /**
   * @covers ::appendComponent
   * @covers ::getNextHighestWeight
   * @covers ::getComponentsByRegion
   */
  public function testAppendComponent() {
    $expected = [
      'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', [
        'id' => 'existing-block-id',
      ]))->setWeight(0),
      'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', [
        'id' => 'second-block-id',
      ]))->setWeight(3),
      'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', [
        'id' => 'first-block-id',
      ]))->setWeight(2),
      'new-uuid' => (new SectionComponent('new-uuid', 'some-region', []))->setWeight(1),
    ];
    $this->section
      ->appendComponent(new SectionComponent('new-uuid', 'some-region'));
    $this->assertComponents($expected, $this->section);
  }
  
  /**
   * @covers ::insertAfterComponent
   */
  public function testInsertAfterComponent() {
    $expected = [
      'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', [
        'id' => 'existing-block-id',
      ]))->setWeight(0),
      'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', [
        'id' => 'second-block-id',
      ]))->setWeight(4),
      'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', [
        'id' => 'first-block-id',
      ]))->setWeight(2),
      'new-uuid' => (new SectionComponent('new-uuid', 'ordered-region', []))->setWeight(3),
    ];
    $this->section
      ->insertAfterComponent('first-uuid', new SectionComponent('new-uuid', 'ordered-region'));
    $this->assertComponents($expected, $this->section);
  }
  
  /**
   * @covers ::insertAfterComponent
   */
  public function testInsertAfterComponentValidUuidRegionMismatch() {
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('Invalid preceding UUID "existing-uuid"');
    $this->section
      ->insertAfterComponent('existing-uuid', new SectionComponent('new-uuid', 'ordered-region'));
  }
  
  /**
   * @covers ::insertAfterComponent
   */
  public function testInsertAfterComponentInvalidUuid() {
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('Invalid preceding UUID "invalid-uuid"');
    $this->section
      ->insertAfterComponent('invalid-uuid', new SectionComponent('new-uuid', 'ordered-region'));
  }
  
  /**
   * @covers ::insertComponent
   * @covers ::getComponentsByRegion
   */
  public function testInsertComponent() {
    $expected = [
      'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', [
        'id' => 'existing-block-id',
      ]))->setWeight(0),
      'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', [
        'id' => 'second-block-id',
      ]))->setWeight(4),
      'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', [
        'id' => 'first-block-id',
      ]))->setWeight(3),
      'new-uuid' => (new SectionComponent('new-uuid', 'ordered-region', []))->setWeight(2),
    ];
    $this->section
      ->insertComponent(0, new SectionComponent('new-uuid', 'ordered-region'));
    $this->assertComponents($expected, $this->section);
  }
  
  /**
   * @covers ::insertComponent
   */
  public function testInsertComponentAppend() {
    $expected = [
      'existing-uuid' => (new SectionComponent('existing-uuid', 'some-region', [
        'id' => 'existing-block-id',
      ]))->setWeight(0),
      'second-uuid' => (new SectionComponent('second-uuid', 'ordered-region', [
        'id' => 'second-block-id',
      ]))->setWeight(3),
      'first-uuid' => (new SectionComponent('first-uuid', 'ordered-region', [
        'id' => 'first-block-id',
      ]))->setWeight(2),
      'new-uuid' => (new SectionComponent('new-uuid', 'ordered-region', []))->setWeight(4),
    ];
    $this->section
      ->insertComponent(2, new SectionComponent('new-uuid', 'ordered-region'));
    $this->assertComponents($expected, $this->section);
  }
  
  /**
   * @covers ::insertComponent
   */
  public function testInsertComponentInvalidDelta() {
    $this->expectException(\OutOfBoundsException::class);
    $this->expectExceptionMessage('Invalid delta "7" for the "new-uuid" component');
    $this->section
      ->insertComponent(7, new SectionComponent('new-uuid', 'ordered-region'));
  }
  
  /**
   * Asserts that the section has the expected components.
   *
   * @param \Drupal\layout_builder\SectionComponent[] $expected
   *   The expected sections.
   * @param \Drupal\layout_builder\Section $section
   *   The section storage to check.
   *
   * @internal
   */
  protected function assertComponents(array $expected, Section $section) : void {
    $result = $section->getComponents();
    $this->assertEquals($expected, $result);
    $this->assertSame(array_keys($expected), array_keys($result));
  }
  
  /**
   * @covers ::getThirdPartySettings
   * @dataProvider providerTestGetThirdPartySettings
   */
  public function testGetThirdPartySettings($provider, $expected) {
    $this->assertSame($expected, $this->section
      ->getThirdPartySettings($provider));
  }
  
  /**
   * Provides test data for ::testGetThirdPartySettings().
   */
  public function providerTestGetThirdPartySettings() {
    $data = [];
    $data[] = [
      'bad_judgement',
      [
        'blink_speed' => 'fast',
        'spin_direction' => 'clockwise',
      ],
    ];
    $data[] = [
      'hunt_and_peck',
      [
        'delay' => '300ms',
      ],
    ];
    $data[] = [
      'non_existing_provider',
      [],
    ];
    return $data;
  }
  
  /**
   * @covers ::getThirdPartySetting
   * @dataProvider providerTestGetThirdPartySetting
   */
  public function testGetThirdPartySetting($provider, $key, $expected, $default = FALSE) {
    if ($default) {
      $this->assertSame($expected, $this->section
        ->getThirdPartySetting($provider, $key, $default));
    }
    else {
      $this->assertSame($expected, $this->section
        ->getThirdPartySetting($provider, $key));
    }
  }
  
  /**
   * Provides test data for ::testGetThirdPartySetting().
   */
  public function providerTestGetThirdPartySetting() {
    $data = [];
    $data[] = [
      'bad_judgement',
      'blink_speed',
      'fast',
    ];
    $data[] = [
      'hunt_and_peck',
      'delay',
      '300ms',
    ];
    $data[] = [
      'hunt_and_peck',
      'non_existing_key',
      NULL,
    ];
    $data[] = [
      'non_existing_provider',
      'non_existing_key',
      NULL,
    ];
    $data[] = [
      'non_existing_provider',
      'non_existing_key',
      'default value',
      'default value',
    ];
    return $data;
  }
  
  /**
   * @covers ::setThirdPartySetting
   * @dataProvider providerTestSetThirdPartySetting
   */
  public function testSetThirdPartySetting($provider, $key, $value, $expected) {
    $this->section
      ->setThirdPartySetting($provider, $key, $value);
    $this->assertSame($expected, $this->section
      ->getThirdPartySettings($provider));
  }
  
  /**
   * Provides test data for ::testSetThirdPartySettings().
   */
  public function providerTestSetThirdPartySetting() {
    $data = [];
    $data[] = [
      'bad_judgement',
      'blink_speed',
      'super fast',
      [
        'blink_speed' => 'super fast',
        'spin_direction' => 'clockwise',
      ],
    ];
    $data[] = [
      'bad_judgement',
      'new_setting',
      'new_value',
      [
        'blink_speed' => 'fast',
        'spin_direction' => 'clockwise',
        'new_setting' => 'new_value',
      ],
    ];
    $data[] = [
      'new_provider',
      'new_setting',
      'new_value',
      [
        'new_setting' => 'new_value',
      ],
    ];
    return $data;
  }
  
  /**
   * @covers ::unsetThirdPartySetting
   * @dataProvider providerTestUnsetThirdPartySetting
   */
  public function testUnsetThirdPartySetting($provider, $key, $expected) {
    $this->section
      ->unsetThirdPartySetting($provider, $key);
    $this->assertSame($expected, $this->section
      ->getThirdPartySettings($provider));
  }
  
  /**
   * Provides test data for ::testUnsetThirdPartySetting().
   */
  public function providerTestUnsetThirdPartySetting() {
    $data = [];
    $data['Key with values'] = [
      'bad_judgement',
      'blink_speed',
      [
        'spin_direction' => 'clockwise',
      ],
    ];
    $data['Key without values'] = [
      'hunt_and_peck',
      'delay',
      [],
    ];
    $data['Non-existing key'] = [
      'bad_judgement',
      'non_existing_key',
      [
        'blink_speed' => 'fast',
        'spin_direction' => 'clockwise',
      ],
    ];
    $data['Non-existing provider'] = [
      'non_existing_provider',
      'non_existing_key',
      [],
    ];
    return $data;
  }
  
  /**
   * @covers ::getThirdPartyProviders
   */
  public function testGetThirdPartyProviders() {
    $this->assertSame([
      'bad_judgement',
      'hunt_and_peck',
    ], $this->section
      ->getThirdPartyProviders());
    $this->section
      ->unsetThirdPartySetting('hunt_and_peck', 'delay');
    $this->assertSame([
      'bad_judgement',
    ], $this->section
      ->getThirdPartyProviders());
  }
  
  /**
   * @covers ::getLayout
   * @dataProvider providerTestGetLayout
   */
  public function testGetLayout(array $contexts, bool $should_context_apply) {
    $layout = $this->prophesize(LayoutInterface::class);
    $layout_plugin_manager = $this->prophesize(LayoutPluginManagerInterface::class);
    $layout_plugin_manager->createInstance('layout_onecol', [])
      ->willReturn($layout->reveal());
    $context_handler = $this->prophesize(ContextHandlerInterface::class);
    if ($should_context_apply) {
      $context_handler->applyContextMapping($layout->reveal(), $contexts)
        ->shouldBeCalled();
    }
    else {
      $context_handler->applyContextMapping($layout->reveal(), $contexts)
        ->shouldNotBeCalled();
    }
    $container = new ContainerBuilder();
    $container->set('plugin.manager.core.layout', $layout_plugin_manager->reveal());
    $container->set('context.handler', $context_handler->reveal());
    \Drupal::setContainer($container);
    $output = $this->section
      ->getLayout($contexts);
    $this->assertSame($layout->reveal(), $output);
  }
  
  /**
   * Provides test data for ::testGetLayout().
   */
  public function providerTestGetLayout() {
    $data = [];
    $data['contexts'] = [
      [
        'foo' => 'bar',
      ],
      TRUE,
    ];
    $data['no contexts'] = [
      [],
      FALSE,
    ];
    return $data;
  }
}Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides | 
|---|---|---|---|---|---|---|
| PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
| PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
| SectionTest::$section | protected | property | The section object to test. | |||
| SectionTest::assertComponents | protected | function | Asserts that the section has the expected components. | |||
| SectionTest::providerTestGetLayout | public | function | Provides test data for ::testGetLayout(). | |||
| SectionTest::providerTestGetThirdPartySetting | public | function | Provides test data for ::testGetThirdPartySetting(). | |||
| SectionTest::providerTestGetThirdPartySettings | public | function | Provides test data for ::testGetThirdPartySettings(). | |||
| SectionTest::providerTestSetThirdPartySetting | public | function | Provides test data for ::testSetThirdPartySettings(). | |||
| SectionTest::providerTestUnsetThirdPartySetting | public | function | Provides test data for ::testUnsetThirdPartySetting(). | |||
| SectionTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
| SectionTest::testAppendComponent | public | function | @covers ::appendComponent[[api-linebreak]] @covers ::getNextHighestWeight[[api-linebreak]] @covers ::getComponentsByRegion[[api-linebreak]] | |||
| SectionTest::testGetComponent | public | function | @covers ::getComponent[[api-linebreak]] | |||
| SectionTest::testGetComponentInvalidUuid | public | function | @covers ::getComponent[[api-linebreak]] | |||
| SectionTest::testGetComponents | public | function | @covers ::__construct[[api-linebreak]] @covers ::setComponent[[api-linebreak]] @covers ::getComponents[[api-linebreak]] | |||
| SectionTest::testGetLayout | public | function | @covers ::getLayout[[api-linebreak]] @dataProvider providerTestGetLayout | |||
| SectionTest::testGetThirdPartyProviders | public | function | @covers ::getThirdPartyProviders[[api-linebreak]] | |||
| SectionTest::testGetThirdPartySetting | public | function | @covers ::getThirdPartySetting[[api-linebreak]] @dataProvider providerTestGetThirdPartySetting | |||
| SectionTest::testGetThirdPartySettings | public | function | @covers ::getThirdPartySettings[[api-linebreak]] @dataProvider providerTestGetThirdPartySettings | |||
| SectionTest::testInsertAfterComponent | public | function | @covers ::insertAfterComponent[[api-linebreak]] | |||
| SectionTest::testInsertAfterComponentInvalidUuid | public | function | @covers ::insertAfterComponent[[api-linebreak]] | |||
| SectionTest::testInsertAfterComponentValidUuidRegionMismatch | public | function | @covers ::insertAfterComponent[[api-linebreak]] | |||
| SectionTest::testInsertComponent | public | function | @covers ::insertComponent[[api-linebreak]] @covers ::getComponentsByRegion[[api-linebreak]] | |||
| SectionTest::testInsertComponentAppend | public | function | @covers ::insertComponent[[api-linebreak]] | |||
| SectionTest::testInsertComponentInvalidDelta | public | function | @covers ::insertComponent[[api-linebreak]] | |||
| SectionTest::testRemoveComponent | public | function | @covers ::removeComponent[[api-linebreak]] @covers ::getComponentsByRegion[[api-linebreak]] | |||
| SectionTest::testSetThirdPartySetting | public | function | @covers ::setThirdPartySetting[[api-linebreak]] @dataProvider providerTestSetThirdPartySetting | |||
| SectionTest::testUnsetThirdPartySetting | public | function | @covers ::unsetThirdPartySetting[[api-linebreak]] @dataProvider providerTestUnsetThirdPartySetting | |||
| UnitTestCase::$randomGenerator | protected | property | The random generator. | |||
| UnitTestCase::$root | protected | property | The app root. | 1 | ||
| UnitTestCase::assertArrayEquals | Deprecated | protected | function | Asserts if two arrays are equal by sorting them first. | ||
| 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. | |||
| UnitTestCase::setUpBeforeClass | public static | function | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
