skinr-8.x-2.0-alpha2/skinr_ui/src/Tests/skinr_ui.test

skinr_ui/src/Tests/skinr_ui.test
<?php

/**
 * @file
 * Tests for the Skinr UI module.
 */

/**
 * Base class for Skinr UI tests.
 */
class SkinrUITestCase extends DrupalWebTestCase {
  protected $profile = 'testing';

  /**
   *
   */
  public function setUp() {
    $modules = func_get_args();
    if (isset($modules[0]) && is_array($modules[0])) {
      $modules = $modules[0];
    }
    parent::setUp(array_merge(['block', 'comment', 'contextual', 'skinr_ui', 'skinr_ui_test'], $modules));

    $this->admin_user = $this->drupalCreateUser([
      'administer blocks',
      'access contextual links',
      'administer skinr',
      'edit skin settings',
      'edit advanced skin settings',
    ]);
    $this->drupalLogin($this->admin_user);

    // Enable main system block for content region and the user menu block for
    // the first sidebar.
    // @see http://drupal.org/node/913086
    $default_theme = variable_get('theme_default', 'bartik');
    db_merge('block')
      ->key([
        'theme' => $default_theme,
        'module' => 'system',
        'delta' => 'main',
      ])
      ->fields([
        'status' => 1,
        'region' => 'content',
        'pages' => '',
      ])
      ->execute();
    db_merge('block')
      ->key([
        'theme' => $default_theme,
        'module' => 'system',
        'delta' => 'user-menu',
      ])
      ->fields([
        'status' => 1,
        'region' => 'sidebar_first',
        'pages' => '',
      ])
      ->execute();
    db_merge('block')
      ->key([
        'theme' => $default_theme,
        'module' => 'search',
        'delta' => 'form',
      ])
      ->fields([
        'status' => 1,
        'region' => 'sidebar_first',
        'pages' => '',
      ])
      ->execute();
  }

  /**
   * Asserts that a class is set for the given element id.
   *
   * @param $id
   *   Id of the HTML element to check.
   * @param $class
   *   The class name to check for.
   * @param $message
   *   Message to display.
   *
   * @return
   *   TRUE on pass, FALSE on fail.
   */
  public function assertSkinrClass($id, $class, $message = '') {
    $elements = $this->xpath('//div[@id=:id]', [':id' => $id]);
    $class_attr = (string) $elements[0]['class'];
    $this->assertTrue(strpos($class_attr, ' ' . $class . ' '), $message);
  }

  /**
   * Asserts that a class is not set for the given element id.
   *
   * @param $id
   *   Id of the HTML element to check.
   * @param $class
   *   The class name to check for.
   * @param $message
   *   Message to display.
   *
   * @return
   *   TRUE on pass, FALSE on fail.
   */
  public function assertNoSkinrClass($id, $class, $message = '') {
    $elements = $this->xpath('//div[@id=:id]', [':id' => $id]);
    $class_attr = (string) $elements[0]['class'];
    $this->assertFalse(strpos($class_attr, ' ' . $class . ' '), $message);
  }

}

/**
 * Tests UI functionality.
 */
class SkinrUIBasicTestCase extends SkinrUITestCase {

  /**
   *
   */
  public static function getInfo() {
    return [
      'name' => 'UI',
      'description' => 'Tests basic Skinr UI functionality.',
      'group' => 'Skinr',
    ];
  }

  /**
   * Tests basic configuration and applying of a skin.
   *
   * @todo Remove the overly verbose inline comments after the Skinr development
   *   team has figured out how to write tests.
   */
  public function testSkinEdit() {
    // Go to the front page, on which the user menu block should appear.
    $this->drupalGet('');
    // Click the first (index 0) 'Edit skin' link on the page, which should be
    // the link in the contextual links of the user menu block, since no other
    // skinnable elements are visible on the page.
    // For now, this is a simple way to assert and access Skinr links. In the
    // future, we want to be more explicit in testing; i.e., verify that there
    // is really only this link, its 'href' is correct, that it appears in the
    // right location, etc.pp; DrupalWebTestCase ($this) provides many helper
    // functions to assert such things.
    $this->clickLink(t('Edit skin'), 0);
    // Verify that we end up on the expected URL to configure skins for the
    // user menu block.
    $front = variable_get('site_frontpage', 'node');
    $this->assertUrl('admin/structure/skinr/edit/block/system__user-menu/configure', [
      'query' => ['destination' => $front],
    ]);

    // skinr_ui_test.module got enabled in setUp(), so its skins should be
    // available.
    // Verify that we can apply the skinr_ui_test_border skin to the block.
    $edit = [
      'skinr_settings[bartik][groups][general][skinr_ui_test_bgcolor]' => 'bgcolor_red',
    ];
    // NULL means that we want to post to the page that is still contained in
    // SimpleTest's internal browser; i.e., the page of the path above. Instead
    // of passing NULL, you can also pass a Drupal system path and SimpleTest
    // will automatically do a $this->drupalGet($path) for you before posting.
    $this->drupalPost(NULL, $edit, t('Save'));

    // After posting, we expect to be redirected to the originating page, due
    // to the 'destination' query parameter in the 'Edit skin' link. Since we
    // came from the front page, Drupal will redirect us to the actual path of
    // the front page, not ''.
    // Verify that we were redirected to the originating page.
    $this->assertUrl($front);

    // Verify that the skin has been applied.
    $this->assertSkinrClass('block-system-user-menu', 'bgcolor-red', 'CSS class of configured skin option found.');
  }

  /**
   * Tests access control for editing additional CSS classes.
   */
  public function testSkinAdditionalEdit() {
    // Verify that we can apply additional CSS classes.
    $edit = [
      'skinr_settings[bartik][groups][_additional][_additional]' => 'additional',
    ];
    $this->drupalPost('admin/structure/skinr/edit/block/system__user-menu/configure', $edit, t('Save'));

    // Verify that the skin has been applied.
    $this->drupalGet('');
    $this->assertSkinrClass('block-system-user-menu', 'additional', 'Additional CSS class <em>additional</em> of configured skin option found.');

    // Now let's check the same for a user that has no access to alter this.
    $user = $this->drupalCreateUser(['edit skin settings']);
    $this->drupalLogin($user);

    // Verify that the additional CSS classes field is not enabled.
    $this->drupalGet('admin/structure/skinr/edit/block/system__user-menu/configure');
    $this->assertNoFieldByName('skinr_settings[bartik][groups][_additional][_additional]', NULL, 'Additional CSS classes field is not enabled for this user.');

    // Save form when additional CSS classes is not set.
    $edit = [];
    $this->drupalPost(NULL, $edit, t('Save'));

    // Verify that the old class is still applied.
    $this->drupalGet('');
    $this->assertSkinrClass('block-system-user-menu', 'additional', 'Additional CSS class <em>additional</em> of configured skin option found.');
  }

  /**
   * Tests output of widgets on the skin configuration form.
   */
  public function testSkinEditWidgets() {
    // Go to the edit page for system__user_menu block.
    $this->drupalGet('admin/structure/skinr/library');
    $this->drupalGet('admin/structure/skinr/edit/block/system__user-menu/configure');

    // Check for output from empty groups.
    $this->assertNoRaw('id="edit-skinr-settings-block-group-bartik-box"', 'Resulting empty group is not displayed.');

    // Check the widgets.
    $this->assertFieldByName('skinr_settings[bartik][groups][general][skinr_ui_test_bgcolor]', NULL, 'Widget with valid type is displayed.');
    $this->assertNoFieldByName('skinr_settings[bartik][groups][box][skinr_ui_test_border]', NULL, 'Widget with invalid type is not displayed.');

    // Custom widget with form callback.
    $this->assertFieldByName('skinr_settings[bartik][groups][general][skinr_ui_test_custom][custom]', NULL, 'Widget with form callback is displayed.');
    $this->assertNoFieldChecked('edit-skinr-settings-bartik-groups-general-skinr-ui-test-custom-custom', 'Value for custom widget empty.');

    // Now make sure default values work for custom widgets.
    $skin = (object) [
      'theme' => 'bartik',
      'module' => 'block',
      'element' => 'system__user-menu',
      'skin' => 'skinr_ui_test_custom',
      'options' => ['custom'],
      'status' => 1,
    ];
    skinr_skin_save($skin);

    $this->drupalGet('admin/structure/skinr/edit/block/system__user-menu/configure');
    $this->assertFieldChecked('edit-skinr-settings-bartik-groups-general-skinr-ui-test-custom-custom', 'Value for custom widget set.');
  }

  /**
   * Tests access control for editing additional CSS classes.
   */
  public function testSkinEditThemeHooks() {
    // Widget should appear for system blocks.
    $this->drupalGet('admin/structure/skinr/edit/block/system__user-menu/configure');
    $this->assertField('edit-skinr-settings-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to system blocks, appeared on the configuration form for system\'s user-menu block.');

    // Widget should not appear search blocks.
    $this->drupalGet('admin/structure/skinr/edit/block/search__form/configure');
    $this->assertNoField('edit-skinr-settings-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to system blocks, did not appear on the configuration form for search\'s form block.');

    // Widget should appear for page node comments.
    $this->drupalGet('admin/structure/skinr/edit/comment/page/configure');
    $this->assertField('edit-skinr-settings-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to page node comments, appeared on the configuration form for page node comments.');

    // Widget should not appear for article node comments.
    $this->drupalGet('admin/structure/skinr/edit/comment/article/configure');
    $this->assertNoField('edit-skinr-settings-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to page node comments, did not appear on the configuration form for article node comments.');

    // Widget should appear for page nodes.
    $this->drupalGet('admin/structure/skinr/edit/node/page/configure');
    $this->assertField('edit-skinr-settings-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to page node types, appeared on the configuration form for page node types.');

    // Widget should not appear for article nodes.
    $this->drupalGet('admin/structure/skinr/edit/node/article/configure');
    $this->assertNoField('edit-skinr-settings-bartik-groups-general-skinr-ui-test-color-color-white', 'The widget, which is limited to page node types, did not appear on the configuration form for article node types.');
  }

}

/**
 * Tests whether or not elements are skinable in Skinr UI.
 */
class SkinrUISkinableTestCase extends DrupalWebTestCase {

  /**
   *
   */
  public static function getInfo() {
    return [
      'name' => 'UI - Is Skinable',
      'description' => 'Tests whether or not elements are skinable in Skinr UI.',
      'group' => 'Skinr',
    ];
  }

  /**
   *
   */
  public function setUp() {
    parent::setUp(['skinr_ui_test_skinable']);

    $this->admin_user = $this->drupalCreateUser([
      'administer blocks',
      'access contextual links',
      'administer skinr',
      'edit skin settings',
      'edit advanced skin settings',
    ]);
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Tests if hooks are properly identified as skinable.
   */
  public function testHookSkinable() {
    // This test doesn't include the * (all) theme hook option.
    // Only page nodes are allowed.
    $this->drupalGet('skinr-ui-test/element-skinable/node/page');
    $this->assertText('true', t('Element node__page is skinable.'));
    $this->drupalGet('skinr-ui-test/element-skinable/node/article');
    $this->assertText('false', t('Element node__article is not skinable.'));

    // All blocks are allowed.
    $this->drupalGet('skinr-ui-test/element-skinable/block/system__navigation');
    $this->assertText('true', t('Element block__system__navigation is skinable.'));

    // Only page comments are allowed.
    $this->drupalGet('skinr-ui-test/element-skinable/comment/page');
    $this->assertText('true', t('Element comment__page is skinable.'));
    $this->drupalGet('skinr-ui-test/element-skinable/comment/panel');
    $this->assertText('false', t('Element comment__panel is not skinable.'));

    // No regions are allowed.
    $this->drupalGet('skinr-ui-test/element-skinable/system/region__sidebar_first');
    $this->assertText('false', t('Element region__sidebar_first is not skinable.'));
  }

}

/**
 * Tests administrative pages functionality.
 */
class SkinrUIAdminTestCase extends SkinrUITestCase {

  /**
   *
   */
  public static function getInfo() {
    return [
      'name' => 'UI - Administration',
      'description' => 'Tests administrative Skinr UI functionality.',
      'group' => 'Skinr',
    ];
  }

  /**
   *
   */
  public function setUp() {
    parent::setUp(['skinr_test', 'skinr_test_default']);

    $this->admin_user = $this->drupalCreateUser([
      'administer skinr',
      'edit skin settings',
      'edit advanced skin settings',
    ]);
    $this->drupalLogin($this->admin_user);

    // Enable Garland and skinr_test_subtheme without enabling its base theme in
    // order to test subtheme inheritance functionality.
    theme_enable(['garland', 'skinr_test_subtheme']);
  }

  /**
   * Tests default status of skins.
   *
   * The skinr_test_basetheme skin defined by the skinr_test_basetheme theme
   * specifies a default status for itself. Its subtheme should inherit the
   * status of the basetheme.
   *
   * @todo Add assertions for 'default status' itself.
   */
  public function testSkinDefaultStatus() {
    // Verify that it is enabled for the skinr_test_subtheme.
    $this->drupalGet('admin/structure/skinr/library/list/skinr_test_subtheme');
    $this->assertFieldChecked('edit-skins-general-skinr-test-basetheme-enable', 'skinr_test_basetheme skin is enabled for skinr_test_subtheme.');

    // Verify that it is disabled for Bartik by default.
    $this->drupalGet('admin/structure/skinr/library/list/bartik');
    $this->assertNoFieldChecked('edit-skins-general-skinr-test-basetheme-enable', 'skinr_test_basetheme skin is disabled for Bartik.');

    // Verify that it is disabled for Garland by default.
    $this->drupalGet('admin/structure/skinr/library/list/garland');
    $this->assertNoFieldChecked('edit-skins-general-skinr-test-basetheme-enable', 'skinr_test_basetheme skin is disabled for Garland.');

    // Override the status for skinr_test_subtheme and Bartik, then verify them.
    $skin = (object) [
      'theme' => 'skinr_test_subtheme',
      'module' => 'block',
      'element' => 'system__user-menu',
      'skin' => 'skinr_test_subtheme',
      'options' => ['option1', 'option2'],
      'status' => 1,
    ];
    skinr_skin_save($skin);
    $skin = entity_load('skin', $skin->sid);

    // Override the default skin.
    $skin->element = 'system-main';
    $this->drupalGet('admin/structure/skinr');
    $this->clickLink(t('disable'), 1);
    // Unaltered skin configuration object should have been saved with only the status updated.
    // Load an uncached version of the skin configuration object.
    $skin = entity_load('skin', $skin->sid, TRUE);
    $this->assertFalse($skin->status, 'Status was disabled successfully.');
    $this->assertEqual($skin->element, 'system__user-menu', 'Only status was updated, even though the object was modified before updating status.');

    // Enable the skin configuration.
    $this->drupalGet('admin/structure/skinr');
    $this->clickLink(t('enable'), 0);
    // Load an uncached version of the skin configuration object.
    $skin = entity_load('skin', $skin->sid, TRUE);
    $this->assertTrue($skin->status, 'Status was enabled successfully.');
  }

  /**
   * Tests skin group functionality.
   */
  public function testSkinGroups() {
    $this->drupalGet('admin/structure/skinr/library');

    // Verify that the 'General' (default) group appears.
    $this->assertText(t('General'));

    // Verify that the 'Box styles' group appears, since skinr_ui_test module
    // registers a skin in that group.
    $this->assertText(t('Box styles'));
  }

  /**
   * Tests skin configuration listing functionality.
   */
  public function testSkinListing() {
    $skin = (object) [
      'theme' => 'skinr_test_subtheme',
      'module' => 'block',
      'element' => 'system__user-menu',
      'skin' => 'skinr_test_subtheme',
      'options' => ['option1', 'option2'],
      'status' => 1,
    ];
    skinr_skin_save($skin);

    // Verify that the skin configuration appears on the skin configurations
    // overview page.
    $this->drupalGet('admin/structure/skinr');
    $this->assertLinkByHref('admin/structure/skinr/skin/' . $skin->sid . '/delete', 0, 'Skin configuration was found on overview page.');

    // Test that revert link doesn't appear for default skin configurations.
    $default_skin = skinr_skin_load_by_uuid('501ff0c3-db03-0944-9910-3a788f38097a');
    $this->assertNoLinkByHref('admin/structure/skinr/skin/' . $default_skin->sid . '/revert', 0, 'No revert operation is available for default skin configuration.');

    // Test that delete link does not appear for default skin configurations.
    $this->assertNoLinkByHref('admin/structure/skinr/skin/' . $default_skin->sid . '/delete', 0, 'No delete operation is available for default skin configuration.');

    // Test that revert link appears for skin configurations in code that are
    // overridden in databse.
    $default_skin->options = ['options3'];
    skinr_skin_save($default_skin);
    $this->drupalGet('admin/structure/skinr');
    $this->assertLinkByHref('admin/structure/skinr/skin/' . $default_skin->sid . '/revert', 0, 'Revert operation is available for overridden skin configuration.');

    // Test that delete link does not appear for overridden skin configurations.
    $this->assertNoLinkByHref('admin/structure/skinr/skin/' . $default_skin->sid . '/delete', 0, 'No delete operation is available for overridden skin configuration.');

    // Test reverting overridden skin.
    $this->clickLink(t('revert'), 0);
    $this->drupalPost(NULL, [], t('Revert'));

    // Load an uncached version of the skin configuration object.
    $default_skin = entity_load('skin', $default_skin->sid, TRUE);
    $this->assertTrue(skinr_skin_storage($default_skin) == SKINR_STORAGE_IN_CODE, 'Overridden skin configuration was reverted to default.');

    // @todo Should we check the filtering and update options functionality?
  }

  /**
   * Tests skin configuration import and export forms.
   */
  public function testSkinImportExport() {
    $this->drupalGet('admin/structure/skinr/import');

    $uuid1 = '16daa322-5ac0-49e4-cda2-809a13bb965b';
    $uuid2 = '7384adb8-50a8-67d4-2dcd-5acda9b5c76e';

    $edit = [
      'skinr_skins' => "\$skin = new stdClass();
\$skin->status = TRUE; /* Edit this to false to make a default skin disabled initially */
\$skin->api_version = " . SKINR_VERSION . ";
\$skin->uuid = '" . $uuid1 . "';
\$skin->theme = 'bartik';
\$skin->module = 'block';
\$skin->element = 'system__main';
\$skin->skin = 'skinr_test_example';
\$skin->options = array(
  'option1' => 'option1',
);
\$skins['" . $uuid1 . "'] = \$skin;

\$skin = new stdClass();
\$skin->status = TRUE; /* Edit this to false to make a default skin disabled initially */
\$skin->api_version = " . SKINR_VERSION . ";
\$skin->uuid = '" . $uuid2 . "';
\$skin->theme = 'bartik';
\$skin->module = 'block';
\$skin->element = 'system__user-menu';
\$skin->skin = 'skinr_test_subtheme';
\$skin->options = array(
  'option1' => 'option1',
  'option2' => 'option2',
);
\$skins['" . $uuid2 . "'] = \$skin;
",
    ];
    $this->drupalPost(NULL, $edit, t('Import'));

    // Now check if the imported skin configurations exist.
    $skin1 = skinr_skin_load_by_uuid($uuid1);
    $skin2 = skinr_skin_load_by_uuid($uuid2);
    $this->assertTrue(isset($skin1->uuid) && $skin1->uuid == $uuid1 && isset($skin2->uuid) && $skin2->uuid == $uuid2, 'Successfully imported skin configurations.');

    // Test export.
    $this->drupalGet('admin/structure/skinr/export');
    $this->drupalPost(NULL, ['theme' => 'bartik'], t('Export'));
    $this->assertFieldByName('skinr_skins', $edit['skinr_skins'], 'Export value is correct.');
  }

}

/**
 * Tests UI functionality for Block plugin.
 */
class SkinrUIPluginTestCase extends DrupalWebTestCase {

  /**
   *
   */
  public static function getInfo() {
    return [
      'name' => 'Plugins UI - Core',
      'description' => 'Tests Skinr UI functionality for functionality plugins from Drupal core.',
      'group' => 'Skinr',
    ];
  }

  /**
   *
   */
  public function setUp() {
    parent::setUp(['block', 'comment', 'node', 'skinr_ui', 'skinr_ui_test']);

    $this->admin_user = $this->drupalCreateUser([
      'administer blocks',
      'access comments',
      'access content',
      'post comments',
      'skip comment approval',

      'access contextual links',
      'administer skinr',
      'edit skin settings',
      'edit advanced skin settings',
      'bypass node access',
    ]);
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Asserts that a select option in the current page exists.
   *
   * @param $name
   *   Id of select field to assert.
   * @param $option
   *   Option to assert.
   * @param $message
   *   Message to display.
   *
   * @return
   *   TRUE on pass, FALSE on fail.
   */
  protected function assertOptionExists($name, $option, $message = '') {
    $elements = $this->xpath('//select[@name=:name]//option[@value=:option]', [':name' => $name, ':option' => $option]);
    return $this->assertTrue(isset($elements[0]), $message ? $message : t('Option @option for field @name exists.', ['@option' => $option, '@name' => $name]), t('Browser'));
  }

  /**
   * Tests block plugin.
   */
  public function testBlock() {
    // Enable user menu block for the first sidebar.
    // @see http://drupal.org/node/913086
    $default_theme = variable_get('theme_default', 'bartik');
    db_merge('block')
      ->key([
        'theme' => $default_theme,
        'module' => 'system',
        'delta' => 'user-menu',
      ])
      ->fields([
        'status' => 1,
        'region' => 'sidebar_first',
        'pages' => '',
      ])
      ->execute();

    // Get front page.
    $this->drupalGet('');

    // Make sure our contextual link appears on the page.
    $this->assertLinkByHref('admin/structure/skinr/edit/block/system__user-menu/configure', 0, 'Contexual link to edit block\'s skin configuration was found.');

    // Make sure this block's options are returned.
    $this->drupalGet('admin/structure/skinr/add');
    $this->assertOptionExists('element', 'system__user-menu', 'User menu block was returned by block_skinr_ui_element_options().');

    // Test the returned element title.
    $skin = (object) [
      'theme' => $default_theme,
      'module' => 'block',
      'element' => 'system__user-menu',
      'skin' => 'skinr_ui_test_bgcolor',
      'options' => ['bgcolor_red'],
      'status' => 1,
    ];
    skinr_skin_save($skin);

    $title = skinr_invoke_all('skinr_ui_element_title', $skin->module, $skin->element, $skin->theme);
    $title = reset($title);
    $this->assertTrue($title == 'User menu', 'Block title was returned.');
  }

  /**
   * Tests comment plugin.
   */
  public function testComment() {
    $default_theme = variable_get('theme_default', 'bartik');

    // Create a node.
    $node1 = $this->drupalCreateNode(['type' => 'page']);

    // Go to node.
    $uri = entity_uri('node', $node1);
    $this->drupalGet($uri['path']);

    // Add a comment to the node. With bartik the contextual links won't
    // display until there is at least one comment.
    $edit = [
      'comment_body[und][0][value]' => $this->randomString(128),
    ];
    $this->drupalPost(NULL, $edit, t('Save'));

    // Make sure our contextual link appears on the page.
    $this->assertLinkByHref('admin/structure/skinr/edit/comment/page/configure', 0, 'Contexual link to edit comment\'s skin configuration was found.');

    // Make sure this block's options are returned.
    $this->drupalGet('admin/structure/skinr/add');
    $this->assertOptionExists('element', 'page', 'Node type was returned by node_skinr_ui_element_options().');

    // Test the returned element title.
    $skin = (object) [
      'theme' => $default_theme,
      'module' => 'comment',
      'element' => 'page',
      'skin' => 'skinr_ui_test_bgcolor',
      'options' => ['bgcolor_red'],
      'status' => 1,
    ];
    skinr_skin_save($skin);

    $title = skinr_invoke_all('skinr_ui_element_title', $skin->module, $skin->element, $skin->theme);
    $title = reset($title);
    $this->assertEqual($title, 'Basic page', 'Node type title was returned.');
  }

  /**
   * Tests node plugin.
   */
  public function testNode() {
    $default_theme = variable_get('theme_default', 'bartik');

    // Create a node.
    $node = $this->drupalCreateNode(['type' => 'article']);

    // Go to node.
    $uri = entity_uri('node', $node);
    $this->drupalGet($uri['path']);

    // Make sure our contextual link appears on the page.
    $this->assertLinkByHref('admin/structure/skinr/edit/node/article/configure', 0, 'Contexual link to edit node\'s skin configuration was found.');

    // Make sure this block's options are returned.
    $this->drupalGet('admin/structure/skinr/add');
    $this->assertOptionExists('element', 'article', 'Node type was returned by node_skinr_ui_element_options().');

    // Test the returned element title.
    $skin = (object) [
      'theme' => $default_theme,
      'module' => 'node',
      'element' => 'article',
      'skin' => 'skinr_ui_test_bgcolor',
      'options' => ['bgcolor_red'],
      'status' => 1,
    ];
    skinr_skin_save($skin);

    $title = skinr_invoke_all('skinr_ui_element_title', $skin->module, $skin->element, $skin->theme);
    $title = reset($title);
    $this->assertEqual($title, 'Article', 'Node type title was returned.');
  }

  /**
   * Tests node plugin.
   */
  public function testSystem() {
    // @todo Add tests for html and region hooks.
  }

}

/**
 * Tests UI functionality for Block plugin.
 */
/**
 * Tests UI functionality for Block plugin.
 */
class SkinrUIPluginViewsTestCase extends DrupalWebTestCase {

  /**
   *
   */
  public static function getInfo() {
    return [
      'name' => 'Plugins UI - Views',
      'description' => 'Tests Skinr UI functionality for functionality plugin from Views.',
      'dependencies' => ['views', 'views_ui'],
      'group' => 'Skinr',
    ];
  }

  /**
   *
   */
  public function setUp() {
    parent::setUp(['views_ui', 'skinr_ui_test']);

    $this->admin_user = $this->drupalCreateUser([
      'administer views',
      'access all views',

      'access contextual links',
      'administer skinr',
      'edit skin settings',
      'edit advanced skin settings',
    ]);
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Asserts that a select option in the current page exists.
   *
   * @param $name
   *   Id of select field to assert.
   * @param $option
   *   Option to assert.
   * @param $message
   *   Message to display.
   *
   * @return
   *   TRUE on pass, FALSE on fail.
   */
  protected function assertOptionExists($name, $option, $message = '') {
    $elements = $this->xpath('//select[@name=:name]//option[@value=:option]', [':name' => $name, ':option' => $option]);
    return $this->assertTrue(isset($elements[0]), $message ? $message : t('Option @option for field @name exists.', ['@option' => $option, '@name' => $name]), t('Browser'));
  }

  /**
   * Tests views plugin.
   */
  public function testViews() {
    $default_theme = variable_get('theme_default', 'bartik');

    // Go to the view's page.
    $this->drupalGet('skinr-ui-test-view');

    // Make sure our contextual link appears on the page.
    $this->assertLinkByHref('admin/structure/skinr/edit/views/skinr_ui_test__page/configure', 0, "Contexual link to edit view's skin configuration was found.");

    // Make sure this view's options are returned.
    $this->drupalGet('admin/structure/skinr/add');
    $this->assertOptionExists('element', 'skinr_ui_test__default', 'Default display for our view was returned by views_skinr_ui_element_options().');
    $this->assertOptionExists('element', 'skinr_ui_test__page', 'Page display for our view was returned by views_skinr_ui_element_options().');

    // Test the returned element title.
    $skin = (object) [
      'theme' => $default_theme,
      'module' => 'views',
      'element' => 'skinr_ui_test__page',
      'skin' => 'skinr_ui_test_bgcolor',
      'options' => ['bgcolor_red'],
      'status' => 1,
    ];
    skinr_skin_save($skin);

    $title = skinr_invoke_all('skinr_ui_element_title', $skin->module, $skin->element, $skin->theme);
    $title = reset($title);
    $this->assertEqual($title, 'Skinr UI Test', 'View title was returned.');
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc