You are here

public function BreakpointAPITest::testConfigName in Breakpoints 8

Test Breakpoint::buildConfigName().

File

lib/Drupal/breakpoint/Tests/BreakpointAPITest.php, line 34
Definition of Drupal\breakpoint\Tests\BreakpointAPITest.

Class

BreakpointAPITest
Tests for general breakpoint API functions.

Namespace

Drupal\breakpoint\Tests

Code

public function testConfigName() {

  // Try an invalid sourceType.
  $breakpoint = entity_create('breakpoint', array(
    'label' => drupal_strtolower($this
      ->randomName()),
    'source' => 'custom_module',
    'sourceType' => 'oops',
  ));
  $exception = FALSE;
  try {
    $breakpoint
      ->save();
  } catch (InvalidBreakpointSourceTypeException $e) {
    $exception = TRUE;
  }
  $this
    ->assertTrue($exception, t('breakpoint_config_name: An exception is thrown when an invalid sourceType is entered.'));

  // Try an invalid source.
  $breakpoint->id = '';
  $breakpoint->sourceType = Breakpoint::SOURCE_TYPE_CUSTOM;
  $breakpoint->source = 'custom*_module source';
  $exception = FALSE;
  try {
    $breakpoint
      ->save();
  } catch (InvalidBreakpointSourceException $e) {
    $exception = TRUE;
  }
  $this
    ->assertTrue($exception, t('breakpoint_config_name: An exception is thrown when an invalid source is entered.'));

  // Try an invalid name (make sure there is at least once capital letter).
  $breakpoint->id = '';
  $breakpoint->source = 'custom_module';
  $breakpoint->name = drupal_ucfirst($this
    ->randomName());
  $exception = FALSE;
  try {
    $breakpoint
      ->save();
  } catch (InvalidBreakpointNameException $e) {
    $exception = TRUE;
  }
  $this
    ->assertTrue($exception, t('breakpoint_config_name: An exception is thrown when an invalid name is entered.'));

  // Try a valid breakpoint.
  $breakpoint->id = '';
  $breakpoint->name = drupal_strtolower($this
    ->randomName());
  $breakpoint->mediaQuery = 'all';
  $exception = FALSE;
  try {
    $breakpoint
      ->save();
  } catch (\Exception $e) {
    $exception = TRUE;
  }
  $this
    ->assertFalse($exception, t('breakpoint_config_name: No exception is thrown when a valid breakpoint is passed.'));
  $this
    ->assertEqual($breakpoint
    ->id(), Breakpoint::SOURCE_TYPE_CUSTOM . '.custom_module.' . $breakpoint->name, t('breakpoint_config_name: A id is set when a valid breakpoint is passed.'));
}