FieldUITest.php 3.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php

/**
 * @file
 * Contains \Drupal\field\Tests\Views\FieldUITest.
 */

namespace Drupal\field\Tests\Views;

/**
 * Tests the UI of the field field handler.
 *
 * @see \Drupal\field\Plugin\views\field\Field
 */
class FieldUITest extends FieldTestBase {

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_view_fieldapi');

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('views_ui');

31 32 33 34 35 36 37 38 39 40
  /**
   * A user with the 'administer views' permission.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $account;

  /**
   * {@inheritdoc}
   */
41 42 43 44 45 46 47 48 49
  public static function getInfo() {
    return array(
      'name' => 'Field: Field handler UI',
      'description' => 'Tests the UI of the field field handler.',
      'group' => 'Views UI'
    );
  }

  /**
50
   * {@inheritdoc}
51
   */
52 53 54 55 56
  public function setUp() {
    parent::setUp();

    $this->account = $this->drupalCreateUser(array('administer views'));
    $this->drupalLogin($this->account);
57 58 59

    $this->setUpFields();
    $this->setUpInstances();
60
  }
61

62 63 64 65
  /**
   * Tests basic field handler settings in the UI.
   */
  public function testHandlerUI() {
66 67 68 69 70 71 72 73 74 75 76 77
    $url = "admin/structure/views/nojs/config-item/test_view_fieldapi/default/field/field_name_0";
    $this->drupalGet($url);

    // Tests the available formatter options.
    $result = $this->xpath('//select[@id=:id]/option', array(':id' => 'edit-options-type'));
    $options = array_map(function($item) {
      return (string) $item->attributes()->value[0];
    }, $result);
    // @todo Replace this sort by assertArray once it's in.
    sort($options, SORT_STRING);
    $this->assertEqual($options, array('text_default', 'text_plain', 'text_trimmed'), 'The text formatters for a simple text field appear as expected.');

78
    $this->drupalPostForm(NULL, array('options[type]' => 'text_trimmed'), t('Apply'));
79 80 81 82 83

    $this->drupalGet($url);
    $this->assertOptionSelected('edit-options-type', 'text_trimmed');

    $random_number = rand(100, 400);
84
    $this->drupalPostForm(NULL, array('options[settings][trim_length]' => $random_number), t('Apply'));
85 86 87 88
    $this->drupalGet($url);
    $this->assertFieldByName('options[settings][trim_length]', $random_number, 'The formatter setting got saved.');

    // Save the view and test whether the settings are saved.
89
    $this->drupalPostForm('admin/structure/views/view/test_view_fieldapi', array(), t('Save'));
90 91 92 93 94 95
    $view = views_get_view('test_view_fieldapi');
    $view->initHandlers();
    $this->assertEqual($view->field['field_name_0']->options['type'], 'text_trimmed');
    $this->assertEqual($view->field['field_name_0']->options['settings']['trim_length'], $random_number);
  }

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  /**
   * Tests the basic field handler form when aggregation is enabled.
   */
  public function testHandlerUIAggregation() {
    // Enable aggregation.
    $edit = array('group_by' => '1');
    $this->drupalPostForm('admin/structure/views/nojs/display/test_view_fieldapi/default/group_by', $edit, t('Apply'));

    $url = "admin/structure/views/nojs/config-item/test_view_fieldapi/default/field/field_name_0";
    $this->drupalGet($url);
    $this->assertResponse(200);

    // Test the click sort column options.
    // Tests the available formatter options.
    $result = $this->xpath('//select[@id=:id]/option', array(':id' => 'edit-options-click-sort-column'));
    $options = array_map(function($item) {
      return (string) $item->attributes()->value[0];
    }, $result);
    sort($options, SORT_STRING);

    $this->assertEqual($options, array('format', 'value'), 'The expected sort field options were found.');
  }

119
}