feeds_mapper.test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @file
  4. * Helper class with auxiliary functions for feeds mapper module tests.
  5. */
  6. /**
  7. * Base class for implementing Feeds Mapper test cases.
  8. */
  9. class FeedsMapperTestCase extends FeedsWebTestCase {
  10. // A lookup map to select the widget for each field type.
  11. private static $field_widgets = array(
  12. 'date' => 'date_text',
  13. 'datestamp' => 'date_text',
  14. 'datetime' => 'date_text',
  15. 'number_decimal' => 'number',
  16. 'email' => 'email_textfield',
  17. 'emimage' => 'emimage_textfields',
  18. 'emaudio' => 'emaudio_textfields',
  19. 'filefield' => 'filefield_widget',
  20. 'image' => 'imagefield_widget',
  21. 'link_field' => 'link_field',
  22. 'number_float' => 'number',
  23. 'number_integer' => 'number',
  24. 'nodereference' => 'nodereference_select',
  25. 'text' => 'text_textfield',
  26. 'userreference' => 'userreference_select',
  27. );
  28. /**
  29. * Assert that a form field for the given field with the given value
  30. * exists in the current form.
  31. *
  32. * @param $field_name
  33. * The name of the field.
  34. * @param $value
  35. * The (raw) value expected for the field.
  36. * @param $index
  37. * The index of the field (for q multi-valued field).
  38. *
  39. * @see FeedsMapperTestCase::getFormFieldsNames()
  40. * @see FeedsMapperTestCase::getFormFieldsValues()
  41. */
  42. protected function assertNodeFieldValue($field_name, $value, $index = 0) {
  43. $names = $this->getFormFieldsNames($field_name, $index);
  44. $values = $this->getFormFieldsValues($field_name, $value);
  45. foreach ($names as $k => $name) {
  46. $value = $values[$k];
  47. $this->assertFieldByName($name, $value, t('Found form field %name for %field_name with the expected value.', array('%name' => $name, '%field_name' => $field_name)));
  48. }
  49. }
  50. /**
  51. * Returns the form fields names for a given CCK field. Default implementation
  52. * provides support for a single form field with the following name pattern
  53. * <code>"field_{$field_name}[{$index}][value]"</code>
  54. *
  55. * @param $field_name
  56. * The name of the CCK field.
  57. * @param $index
  58. * The index of the field (for q multi-valued field).
  59. *
  60. * @return
  61. * An array of form field names.
  62. */
  63. protected function getFormFieldsNames($field_name, $index) {
  64. return array("field_{$field_name}[und][{$index}][value]");
  65. }
  66. /**
  67. * Returns the form fields values for a given CCK field. Default implementation
  68. * returns a single element array with $value casted to a string.
  69. *
  70. * @param $field_name
  71. * The name of the CCK field.
  72. * @param $value
  73. * The (raw) value expected for the CCK field.
  74. * @return An array of form field values.
  75. */
  76. protected function getFormFieldsValues($field_name, $value) {
  77. return array((string)$value);
  78. }
  79. /**
  80. * Create a new content-type, and add a field to it. Mostly copied from
  81. * cck/tests/content.crud.test ContentUICrud::testAddFieldUI
  82. *
  83. * @param $settings
  84. * (Optional) An array of settings to pass through to
  85. * drupalCreateContentType().
  86. * @param $fields
  87. * (Optional) an keyed array of $field_name => $field_type used to add additional
  88. * fields to the new content type.
  89. *
  90. * @return
  91. * The machine name of the new content type.
  92. *
  93. * @see DrupalWebTestCase::drupalCreateContentType()
  94. */
  95. final protected function createContentType(array $settings = array(), array $fields = array()) {
  96. $type = $this->drupalCreateContentType($settings);
  97. $typename = $type->type;
  98. $admin_type_url = 'admin/structure/types/manage/' . str_replace('_', '-', $typename);
  99. // Create the fields
  100. foreach ($fields as $field_name => $options) {
  101. if (is_string($options)) {
  102. $options = array('type' => $options);
  103. }
  104. $field_type = isset($options['type']) ? $options['type'] : 'text';
  105. $field_widget = isset($options['widget']) ? $options['widget'] : $this->selectFieldWidget($field_name, $field_type);
  106. $this->assertTrue($field_widget !== NULL, "Field type $field_type supported");
  107. $label = $field_name . '_' . $field_type . '_label';
  108. $edit = array(
  109. 'fields[_add_new_field][label]' => $label,
  110. 'fields[_add_new_field][field_name]' => $field_name,
  111. 'fields[_add_new_field][type]' => $field_type,
  112. 'fields[_add_new_field][widget_type]' => $field_widget,
  113. );
  114. $this->drupalPost($admin_type_url . '/fields', $edit, 'Save');
  115. // (Default) Configure the field.
  116. $edit = isset($options['settings']) ? $options['settings'] : array();
  117. $this->drupalPost(NULL, $edit, 'Save field settings');
  118. $this->assertText('Updated field ' . $label . ' field settings.');
  119. $edit = isset($options['instance_settings']) ? $options['instance_settings'] : array();
  120. $this->drupalPost(NULL, $edit, 'Save settings');
  121. $this->assertText('Saved ' . $label . ' configuration.');
  122. }
  123. return $typename;
  124. }
  125. /**
  126. * Select the widget for the field. Default implementation provides widgets
  127. * for Date, Number, Text, Node reference, User reference, Email, Emfield,
  128. * Filefield, Image, and Link.
  129. *
  130. * Extracted as a method to allow test implementations to add widgets for
  131. * the tested CCK field type(s). $field_name allow to test the same
  132. * field type with different widget (is this useful ?)
  133. *
  134. * @param $field_name
  135. * The name of the field.
  136. * @param $field_type
  137. * The CCK type of the field.
  138. *
  139. * @return
  140. * The widget for this field, or NULL if the field_type is not
  141. * supported by this test class.
  142. */
  143. protected function selectFieldWidget($field_name, $field_type) {
  144. $field_widgets = FeedsMapperTestCase::$field_widgets;
  145. return isset($field_widgets[$field_type]) ? $field_widgets[$field_type] : NULL;
  146. }
  147. }