feeds_mapper.test 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsMapperTestCase.
  5. */
  6. /**
  7. * Helper class with auxiliary functions for feeds mapper module tests.
  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. 'file' => 'file_generic',
  20. 'image' => 'image_image',
  21. 'link_field' => 'link_field',
  22. 'list_boolean' => 'options_onoff',
  23. 'list_float' => 'options_select',
  24. 'list_integer' => 'options_select',
  25. 'list_text' => 'options_select',
  26. 'number_float' => 'number',
  27. 'number_integer' => 'number',
  28. 'nodereference' => 'nodereference_select',
  29. 'text' => 'text_textfield',
  30. 'text_long' => 'text_textarea',
  31. 'text_with_summary' => 'text_textarea_with_summary',
  32. 'userreference' => 'userreference_select',
  33. );
  34. /**
  35. * Assert that a form field for the given field with the given value
  36. * exists in the current form.
  37. *
  38. * @param $field_name
  39. * The name of the field.
  40. * @param $value
  41. * The (raw) value expected for the field.
  42. * @param $index
  43. * The index of the field (for q multi-valued field).
  44. *
  45. * @see FeedsMapperTestCase::getFormFieldsNames()
  46. * @see FeedsMapperTestCase::getFormFieldsValues()
  47. */
  48. protected function assertNodeFieldValue($field_name, $value, $index = 0) {
  49. $names = $this->getFormFieldsNames($field_name, $index);
  50. $values = $this->getFormFieldsValues($field_name, $value);
  51. foreach ($names as $k => $name) {
  52. $value = $values[$k];
  53. $this->assertFieldByName($name, $value, t('Found form field %name for %field_name with the expected value.', array('%name' => $name, '%field_name' => $field_name)));
  54. }
  55. }
  56. /**
  57. * Assert that a form field for the given field with the given value
  58. * does not exist in the current form.
  59. *
  60. * @param $field_name
  61. * The name of the field.
  62. * @param $value
  63. * The (raw) value of the field.
  64. * @param $index
  65. * The index of the field (for q multi-valued field).
  66. *
  67. * @see FeedsMapperTestCase::getFormFieldsNames()
  68. * @see FeedsMapperTestCase::getFormFieldsValues()
  69. */
  70. protected function assertNoNodeFieldValue($field_name, $value, $index = 0) {
  71. $names = $this->getFormFieldsNames($field_name, $index);
  72. $values = $this->getFormFieldsValues($field_name, $value);
  73. foreach ($names as $k => $name) {
  74. $value = $values[$k];
  75. $this->assertNoFieldByName($name, $value, t('Did not find form field %name for %field_name with the value %value.', array('%name' => $name, '%field_name' => $field_name, '%value' => $value)));
  76. }
  77. }
  78. /**
  79. * Returns the form fields names for a given CCK field. Default implementation
  80. * provides support for a single form field with the following name pattern
  81. * <code>"field_{$field_name}[{$index}][value]"</code>
  82. *
  83. * @param $field_name
  84. * The name of the CCK field.
  85. * @param $index
  86. * The index of the field (for q multi-valued field).
  87. *
  88. * @return
  89. * An array of form field names.
  90. */
  91. protected function getFormFieldsNames($field_name, $index) {
  92. return array("field_{$field_name}[und][{$index}][value]");
  93. }
  94. /**
  95. * Returns the form fields values for a given CCK field. Default implementation
  96. * returns a single element array with $value casted to a string.
  97. *
  98. * @param $field_name
  99. * The name of the CCK field.
  100. * @param $value
  101. * The (raw) value expected for the CCK field.
  102. * @return An array of form field values.
  103. */
  104. protected function getFormFieldsValues($field_name, $value) {
  105. return array((string)$value);
  106. }
  107. /**
  108. * Create a new content-type, and add a field to it. Mostly copied from
  109. * cck/tests/content.crud.test ContentUICrud::testAddFieldUI
  110. *
  111. * @param $settings
  112. * (Optional) An array of settings to pass through to
  113. * drupalCreateContentType().
  114. * @param $fields
  115. * (Optional) an keyed array of $field_name => $field_type used to add additional
  116. * fields to the new content type.
  117. *
  118. * @return
  119. * The machine name of the new content type.
  120. *
  121. * @see DrupalWebTestCase::drupalCreateContentType()
  122. */
  123. final protected function createContentType(array $settings = array(), array $fields = array()) {
  124. $type = $this->drupalCreateContentType($settings);
  125. $typename = $type->type;
  126. $admin_type_url = 'admin/structure/types/manage/' . str_replace('_', '-', $typename);
  127. // Create the fields
  128. foreach ($fields as $field_name => $options) {
  129. if (is_string($options)) {
  130. $options = array('type' => $options);
  131. }
  132. $field_type = isset($options['type']) ? $options['type'] : 'text';
  133. $field_widget = isset($options['widget']) ? $options['widget'] : $this->selectFieldWidget($field_name, $field_type);
  134. $this->assertTrue($field_widget !== NULL, "Field type $field_type supported");
  135. $label = $field_name . '_' . $field_type . '_label';
  136. $edit = array(
  137. 'fields[_add_new_field][label]' => $label,
  138. 'fields[_add_new_field][field_name]' => $field_name,
  139. 'fields[_add_new_field][type]' => $field_type,
  140. 'fields[_add_new_field][widget_type]' => $field_widget,
  141. );
  142. $this->drupalPost($admin_type_url . '/fields', $edit, 'Save');
  143. // (Default) Configure the field.
  144. $edit = isset($options['settings']) ? $options['settings'] : array();
  145. $this->drupalPost(NULL, $edit, 'Save field settings');
  146. $this->assertText('Updated field ' . $label . ' field settings.');
  147. $edit = isset($options['instance_settings']) ? $options['instance_settings'] : array();
  148. $this->drupalPost(NULL, $edit, 'Save settings');
  149. $this->assertText('Saved ' . $label . ' configuration.');
  150. }
  151. return $typename;
  152. }
  153. /**
  154. * Select the widget for the field. Default implementation provides widgets
  155. * for Date, Number, Text, Node reference, User reference, Email, Emfield,
  156. * Filefield, Image, and Link.
  157. *
  158. * Extracted as a method to allow test implementations to add widgets for
  159. * the tested CCK field type(s). $field_name allow to test the same
  160. * field type with different widget (is this useful ?)
  161. *
  162. * @param $field_name
  163. * The name of the field.
  164. * @param $field_type
  165. * The CCK type of the field.
  166. *
  167. * @return
  168. * The widget for this field, or NULL if the field_type is not
  169. * supported by this test class.
  170. */
  171. protected function selectFieldWidget($field_name, $field_type) {
  172. $field_widgets = FeedsMapperTestCase::$field_widgets;
  173. return isset($field_widgets[$field_type]) ? $field_widgets[$field_type] : NULL;
  174. }
  175. }