feeds_mapper_path.test 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsMapperPathTestCase.
  5. */
  6. /**
  7. * Test case for path alias mapper path.inc.
  8. */
  9. class FeedsMapperPathTestCase extends FeedsMapperTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Mapper: Path',
  13. 'description' => 'Test Feeds Mapper support for path aliases.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp(array('path'));
  19. }
  20. /**
  21. * Basic test loading a single entry CSV file.
  22. */
  23. public function testNodeAlias() {
  24. // Create importer configuration.
  25. $this->createImporterConfiguration($this->randomName(), 'path_test');
  26. $this->setPlugin('path_test', 'FeedsFileFetcher');
  27. $this->setPlugin('path_test', 'FeedsCSVParser');
  28. $this->addMappings('path_test', array(
  29. 0 => array(
  30. 'source' => 'Title',
  31. 'target' => 'title',
  32. ),
  33. 1 => array(
  34. 'source' => 'path',
  35. 'target' => 'path_alias',
  36. ),
  37. 2 => array(
  38. 'source' => 'GUID',
  39. 'target' => 'guid',
  40. 'unique' => TRUE,
  41. ),
  42. ));
  43. // Turn on update existing.
  44. $this->setSettings('path_test', 'FeedsNodeProcessor', array('update_existing' => 2));
  45. // Import RSS file.
  46. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  47. $this->assertText('Created 9 nodes');
  48. $aliases = array();
  49. for ($i = 1; $i <= 9; $i++) {
  50. $aliases[] = "path$i";
  51. }
  52. $this->assertAliasCount($aliases);
  53. // Adding a mapping will force update.
  54. $this->addMappings('path_test', array(
  55. 3 => array(
  56. 'source' => 'fake',
  57. 'target' => 'body',
  58. ),
  59. ));
  60. // Import RSS file.
  61. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  62. $this->assertText('Updated 9 nodes');
  63. // Check that duplicate aliases are not created.
  64. $this->assertAliasCount($aliases);
  65. }
  66. /**
  67. * Test support for term aliases.
  68. */
  69. public function testTermAlias() {
  70. // Create importer configuration.
  71. $this->createImporterConfiguration($this->randomName(), 'path_test');
  72. $this->setPlugin('path_test', 'FeedsFileFetcher');
  73. $this->setPlugin('path_test', 'FeedsCSVParser');
  74. $this->setPlugin('path_test', 'FeedsTermProcessor');
  75. // Create vocabulary.
  76. $edit = array(
  77. 'name' => 'Addams vocabulary',
  78. 'machine_name' => 'addams',
  79. );
  80. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  81. $this->setSettings('path_test', 'FeedsTermProcessor', array('bundle' => 'addams', 'update_existing' => 2));
  82. // Add mappings.
  83. $this->addMappings('path_test', array(
  84. 0 => array(
  85. 'source' => 'Title',
  86. 'target' => 'name',
  87. ),
  88. 1 => array(
  89. 'source' => 'path',
  90. 'target' => 'path_alias',
  91. ),
  92. 2 => array(
  93. 'source' => 'GUID',
  94. 'target' => 'guid',
  95. 'unique' => TRUE,
  96. ),
  97. ));
  98. // Import RSS file.
  99. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  100. $this->assertText('Created 9 terms');
  101. $aliases = array();
  102. for ($i = 1; $i <= 9; $i++) {
  103. $aliases[] = "path$i";
  104. }
  105. $this->assertAliasCount($aliases);
  106. // Adding a mapping will force update.
  107. $this->addMappings('path_test', array(
  108. 3 => array(
  109. 'source' => 'fake',
  110. 'target' => 'description',
  111. ),
  112. ));
  113. // Import RSS file.
  114. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  115. $this->assertText('Updated 9 terms');
  116. // Check that duplicate aliases are not created.
  117. $this->assertAliasCount($aliases);
  118. }
  119. public function assertAliasCount($aliases) {
  120. $in_db = db_select('url_alias', 'a')
  121. ->fields('a')
  122. ->condition('a.alias', $aliases)
  123. ->execute()
  124. ->fetchAll();
  125. $this->assertEqual(count($in_db), count($aliases), 'Correct number of aliases in db.');
  126. }
  127. }
  128. /**
  129. * Class for testing Feeds <em>path</em> mapper with pathauto.module.
  130. */
  131. class FeedsMapperPathPathautoTestCase extends FeedsMapperTestCase {
  132. public static function getInfo() {
  133. return array(
  134. 'name' => 'Mapper: Path with pathauto',
  135. 'description' => 'Test Feeds Mapper support for path aliases and pathauto.',
  136. 'group' => 'Feeds',
  137. 'dependencies' => array('pathauto'),
  138. );
  139. }
  140. public function setUp() {
  141. parent::setUp(array('pathauto'));
  142. }
  143. /**
  144. * Basic for allowing pathauto to override the alias.
  145. */
  146. public function test() {
  147. // Create importer configuration.
  148. $this->createImporterConfiguration($this->randomName(), 'path_test');
  149. $this->setPlugin('path_test', 'FeedsFileFetcher');
  150. $this->setPlugin('path_test', 'FeedsCSVParser');
  151. $this->addMappings('path_test', array(
  152. 0 => array(
  153. 'source' => 'Title',
  154. 'target' => 'title',
  155. 'unique' => FALSE,
  156. ),
  157. 1 => array(
  158. 'source' => 'does_not_exist',
  159. 'target' => 'path_alias',
  160. 'pathauto_override' => TRUE,
  161. ),
  162. 2 => array(
  163. 'source' => 'GUID',
  164. 'target' => 'guid',
  165. 'unique' => TRUE,
  166. ),
  167. ));
  168. // Turn on update existing.
  169. $this->setSettings('path_test', 'FeedsNodeProcessor', array('update_existing' => 2));
  170. // Import RSS file.
  171. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  172. $this->assertText('Created 9 nodes');
  173. $aliases = array();
  174. for ($i = 1; $i <= 9; $i++) {
  175. $aliases[] = "content/pathauto$i";
  176. }
  177. $this->assertAliasCount($aliases);
  178. // Adding a mapping will force update.
  179. $this->addMappings('path_test', array(
  180. 3 => array(
  181. 'source' => 'fake',
  182. 'target' => 'body',
  183. ),
  184. ));
  185. // Import RSS file.
  186. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  187. $this->assertText('Updated 9 nodes');
  188. // Check that duplicate aliases are not created.
  189. $this->assertAliasCount($aliases);
  190. }
  191. public function assertAliasCount($aliases) {
  192. $in_db = db_query("SELECT * FROM {url_alias} WHERE alias IN (:aliases)", array(':aliases' => $aliases))->fetchAll();
  193. $this->assertEqual(count($in_db), count($aliases), 'Correct number of aliases in db.');
  194. }
  195. }