feeds_mapper_path.test 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * @file
  4. * Test case for path alias mapper path.inc.
  5. */
  6. /**
  7. * Class for testing Feeds <em>path</em> mapper.
  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('vocabulary' => 'addams'));
  82. // Turn on update existing.
  83. $this->setSettings('path_test', 'FeedsTermProcessor', array('update_existing' => 2));
  84. // Add mappings.
  85. $this->addMappings('path_test', array(
  86. 0 => array(
  87. 'source' => 'Title',
  88. 'target' => 'name',
  89. ),
  90. 1 => array(
  91. 'source' => 'path',
  92. 'target' => 'path_alias',
  93. ),
  94. 2 => array(
  95. 'source' => 'GUID',
  96. 'target' => 'guid',
  97. 'unique' => TRUE,
  98. ),
  99. ));
  100. // Import RSS file.
  101. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  102. $this->assertText('Created 9 terms');
  103. $aliases = array();
  104. for ($i = 1; $i <= 9; $i++) {
  105. $aliases[] = "path$i";
  106. }
  107. $this->assertAliasCount($aliases);
  108. // Adding a mapping will force update.
  109. $this->addMappings('path_test', array(
  110. 3 => array(
  111. 'source' => 'fake',
  112. 'target' => 'description',
  113. ),
  114. ));
  115. // Import RSS file.
  116. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  117. $this->assertText('Updated 9 terms');
  118. // Check that duplicate aliases are not created.
  119. $this->assertAliasCount($aliases);
  120. }
  121. public function assertAliasCount($aliases) {
  122. $in_db = db_select('url_alias', 'a')
  123. ->fields('a')
  124. ->condition('a.alias', $aliases)
  125. ->execute()
  126. ->fetchAll();
  127. $this->assertEqual(count($in_db), count($aliases), 'Correct number of aliases in db.');
  128. }
  129. }
  130. /**
  131. * Class for testing Feeds <em>path</em> mapper with pathauto.module.
  132. */
  133. class FeedsMapperPathPathautoTestCase extends FeedsMapperTestCase {
  134. public static function getInfo() {
  135. return array(
  136. 'name' => 'Mapper: Path with pathauto',
  137. 'description' => 'Test Feeds Mapper support for path aliases and pathauto.',
  138. 'group' => 'Feeds',
  139. 'dependencies' => array('pathauto'),
  140. );
  141. }
  142. public function setUp() {
  143. parent::setUp(array('pathauto'));
  144. }
  145. /**
  146. * Basic for allowing pathauto to override the alias.
  147. */
  148. public function test() {
  149. // Create importer configuration.
  150. $this->createImporterConfiguration($this->randomName(), 'path_test');
  151. $this->setPlugin('path_test', 'FeedsFileFetcher');
  152. $this->setPlugin('path_test', 'FeedsCSVParser');
  153. $this->addMappings('path_test', array(
  154. 0 => array(
  155. 'source' => 'Title',
  156. 'target' => 'title',
  157. 'unique' => FALSE,
  158. ),
  159. 1 => array(
  160. 'source' => 'does_not_exist',
  161. 'target' => 'path_alias',
  162. 'pathauto_override' => TRUE,
  163. ),
  164. 2 => array(
  165. 'source' => 'GUID',
  166. 'target' => 'guid',
  167. 'unique' => TRUE,
  168. ),
  169. ));
  170. // Turn on update existing.
  171. $this->setSettings('path_test', 'FeedsNodeProcessor', array('update_existing' => 2));
  172. // Import RSS file.
  173. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  174. $this->assertText('Created 9 nodes');
  175. $aliases = array();
  176. for ($i = 1; $i <= 9; $i++) {
  177. $aliases[] = "path$i";
  178. }
  179. $this->assertAliasCount($aliases);
  180. // Adding a mapping will force update.
  181. $this->addMappings('path_test', array(
  182. 3 => array(
  183. 'source' => 'fake',
  184. 'target' => 'body',
  185. ),
  186. ));
  187. // Import RSS file.
  188. $this->importFile('path_test', $this->absolutePath() . '/tests/feeds/path_alias.csv');
  189. $this->assertText('Updated 9 nodes');
  190. // Check that duplicate aliases are not created.
  191. $this->assertAliasCount($aliases);
  192. }
  193. public function assertAliasCount($aliases) {
  194. $in_db = db_select('url_alias', 'a')
  195. ->fields('a')
  196. ->condition('a.alias', $aliases)
  197. ->execute()
  198. ->fetchAll();
  199. $this->assertEqual(count($in_db), count($aliases), 'Correct number of aliases in db.');
  200. }
  201. }