QuickEditTestBase.php 3.05 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\quickedit\Tests\QuickEditTestBase.
6 7
 */

8
namespace Drupal\quickedit\Tests;
9 10 11 12

use Drupal\simpletest\DrupalUnitTestBase;

/**
13
 * Base class for testing Quick Edit functionality.
14
 */
15
abstract class QuickEditTestBase extends DrupalUnitTestBase {
16 17 18 19 20 21

  /**
   * Modules to enable.
   *
   * @var array
   */
22
  public static $modules = array('system', 'entity', 'entity_test', 'field', 'field_test', 'filter', 'user', 'text', 'quickedit');
23

24 25 26 27 28 29 30 31 32 33 34 35 36
  /**
   * Bag of created fields and instances.
   *
   * Allows easy access to test field/instance names/IDs/objects via:
   * - $this->fields->{$field_name}_field_storage
   * - $this->fields->{$field_name}_instance
   *
   * @see \Drupal\quickedit\Tests\QuickEditTestBase::createFieldWithInstance()
   *
   * @var \ArrayObject
   */
  protected $fields;

37 38 39
  /**
   * Sets the default field storage backend for fields created during tests.
   */
40
  protected function setUp() {
41 42
    parent::setUp();

43 44
    $this->fields = new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS);

45
    $this->installEntitySchema('entity_test');
46
    $this->installConfig(array('field', 'filter'));
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  }

  /**
   * Creates a field and an instance of it.
   *
   * @param string $field_name
   *   The field name.
   * @param string $type
   *   The field type.
   * @param int $cardinality
   *   The field's cardinality.
   * @param string $label
   *   The field's label (used everywhere: widget label, formatter label).
   * @param array $instance_settings
   * @param string $widget_type
   *   The widget type.
   * @param array $widget_settings
   *   The widget settings.
   * @param string $formatter_type
   *   The formatter type.
   * @param array $formatter_settings
   *   The formatter settings.
   */
70
  protected function createFieldWithInstance($field_name, $type, $cardinality, $label, $instance_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) {
71
    $field_storage = $field_name . '_field_storage';
72
    $this->fields->$field_storage = entity_create('field_storage_config', array(
73 74
      'name' => $field_name,
      'entity_type' => 'entity_test',
75 76
      'type' => $type,
      'cardinality' => $cardinality,
77
    ));
78
    $this->fields->$field_storage->save();
79 80

    $instance = $field_name . '_instance';
81 82
    $this->fields->$instance = entity_create('field_instance_config', array(
      'field_storage' => $this->fields->$field_storage,
83
      'bundle' => 'entity_test',
84 85 86 87
      'label' => $label,
      'description' => $label,
      'weight' => mt_rand(0, 127),
      'settings' => $instance_settings,
88
    ));
89
    $this->fields->$instance->save();
90 91 92

    entity_get_form_display('entity_test', 'entity_test', 'default')
      ->setComponent($field_name, array(
93 94 95
        'type' => $widget_type,
        'label' => $label,
        'settings' => $widget_settings,
96 97
      ))
      ->save();
98

99
    entity_get_display('entity_test', 'entity_test', 'default')
100 101 102 103 104 105 106
      ->setComponent($field_name, array(
        'label' => 'above',
        'type' => $formatter_type,
        'settings' => $formatter_settings
      ))
      ->save();
  }
107

108
}