AlterTest.php 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Definition of Drupal\field_ui\Tests\AlterTest.
 */

namespace Drupal\field_ui\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests custom widget hooks and callbacks on the field administration pages.
 */
class AlterTest extends WebTestBase {
16 17 18 19 20 21 22 23

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('field_ui', 'field_test', 'text', 'options');

24 25 26 27 28 29 30 31 32
  public static function getInfo() {
    return array(
      'name' => 'Widget customization',
      'description' => 'Test custom field widget hooks and callbacks on field administration pages.',
      'group' => 'Field UI',
    );
  }

  function setUp() {
33
    parent::setUp();
34 35 36 37 38 39

    // Create Article node type.
    $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
    $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));

    // Create test user.
40
    $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer node fields', 'administer users', 'administer user fields'));
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    $this->drupalLogin($admin_user);
  }

  /**
   * Tests hook_field_widget_properties_alter() on the default field widget.
   *
   * @see field_test_field_widget_properties_alter()
   * @see field_test_field_widget_properties_user_alter()
   * @see field_test_field_widget_form_alter()
   */
  function testDefaultWidgetPropertiesAlter() {
    // Create the alter_test_text field and an instance on article nodes.
    field_create_field(array(
      'field_name' => 'alter_test_text',
      'type' => 'text',
    ));
57
    $instance = array(
58 59 60 61 62 63 64
      'field_name' => 'alter_test_text',
      'entity_type' => 'node',
      'bundle' => 'article',
      'widget' => array(
        'type' => 'text_textfield',
        'size' => 60,
      ),
65 66
    );
    field_create_instance($instance);
67 68 69 70 71 72

    // Test that field_test_field_widget_properties_alter() sets the size to
    // 42 and that field_test_field_widget_form_alter() reports the correct
    // size when the form is displayed.
    $this->drupalGet('admin/structure/types/manage/article/fields/alter_test_text');
    $this->assertText('Field size: 42', 'Altered field size is found in hook_field_widget_form_alter().');
73 74
    // Test that hook_field_widget_form_alter() registers this is the default
    // value form and sets a message.
75
    $this->assertText('From hook_field_widget_form_alter(): Default form is true.', 'Default value form detected in hook_field_widget_form_alter().');
76 77 78 79 80 81 82

    // Create the alter_test_options field.
    field_create_field(array(
      'field_name' => 'alter_test_options',
      'type' => 'list_text'
    ));
    // Create instances on users and page nodes.
83
    $instance = array(
84 85 86 87 88 89
      'field_name' => 'alter_test_options',
      'entity_type' => 'user',
      'bundle' => 'user',
      'widget' => array(
        'type' => 'options_select',
      )
90 91 92
    );
    field_create_instance($instance);
    $instance = array(
93 94 95 96 97 98
      'field_name' => 'alter_test_options',
      'entity_type' => 'node',
      'bundle' => 'page',
      'widget' => array(
        'type' => 'options_select',
      )
99 100
    );
    field_create_instance($instance);
101 102 103 104 105 106 107 108 109 110 111 112

    // Test that field_test_field_widget_properties_user_alter() replaces
    // the widget and that field_test_field_widget_form_alter() reports the
    // correct widget name when the form is displayed.
    $this->drupalGet('admin/config/people/accounts/fields/alter_test_options');
    $this->assertText('Widget type: options_buttons', 'Widget type is altered for users in hook_field_widget_form_alter().');

    // Test that the widget is not altered on page nodes.
    $this->drupalGet('admin/structure/types/manage/page/fields/alter_test_options');
    $this->assertText('Widget type: options_select', 'Widget type is not altered for pages in hook_field_widget_form_alter().');
  }
}