metatag.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. class MetaTagsTestHelper extends DrupalWebTestCase {
  3. function setUp(array $modules = array()) {
  4. $modules[] = 'ctools';
  5. $modules[] = 'token';
  6. $modules[] = 'metatag';
  7. $modules[] = 'metatag_test';
  8. parent::setUp($modules);
  9. }
  10. }
  11. class MetaTagsNodeUITest extends MetaTagsTestHelper {
  12. /**
  13. * Admin user.
  14. *
  15. * @var \StdClass
  16. */
  17. protected $adminUser;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function getInfo() {
  22. return array(
  23. 'name' => 'Metatag tests for nodes',
  24. 'description' => 'Test Metatag edit functionality for nodes.',
  25. 'group' => 'Metatag',
  26. );
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. function setUp(array $modules = array()) {
  32. $modules = array(
  33. 'metatag_dc',
  34. 'metatag_facebook',
  35. 'metatag_google_plus',
  36. 'metatag_opengraph',
  37. 'metatag_twitter_cards',
  38. );
  39. parent::setUp($modules);
  40. }
  41. /**
  42. * Tests filtering of values using metatag_filter_values_from_defaults().
  43. */
  44. public function testMetatagFilterValuesFromDefaults() {
  45. // Create a content type.
  46. $this->drupalCreateContentType(array(
  47. 'type' => 'metatag_test',
  48. 'name' => 'Test',
  49. ));
  50. // Create an admin-level user.
  51. $this->adminUser = $this->drupalCreateUser(array(
  52. 'administer meta tags',
  53. 'edit meta tags',
  54. 'create metatag_test content',
  55. 'delete any metatag_test content',
  56. 'edit any metatag_test content',
  57. ));
  58. $this->drupalLogin($this->adminUser);
  59. // Assign default values for the new content type.
  60. $this->drupalGet('admin/config/search/metatags/config/add');
  61. $this->drupalPost(NULL, array(
  62. 'instance' => 'node:metatag_test',
  63. ), t('Add and configure'));
  64. $this->drupalPost(NULL, array(
  65. 'metatags[und][dcterms.title][value]' => '[node:title]',
  66. 'metatags[und][dcterms.creator][value]' => '[node:author]',
  67. 'metatags[und][dcterms.date][value]' => '[node:created:custom:Y-m-d\TH:iP]',
  68. 'metatags[und][dcterms.format][value]' => 'text/html',
  69. 'metatags[und][dcterms.identifier][value]' => '[current-page:url:absolute]',
  70. 'metatags[und][dcterms.language][value]' => '[node:language]',
  71. ), t('Save'));
  72. // Create a test node.
  73. $this->drupalGet('node/add/metatag-test');
  74. $this->drupalPost(NULL, array(
  75. 'metatags[und][dcterms.title][value]' => '[node:title] ponies',
  76. 'title' => 'Who likes magic',
  77. ), t('Save'));
  78. // Verify that the node saved correctly.
  79. $xpath = $this->xpath("//h1");
  80. $t_args = array('@type' => 'Test', '%title' => 'Who likes magic');
  81. $this->assertText(strip_tags(t('@type %title has been created.', $t_args)));
  82. // This doesn't work for some reason, it seems the HTML is stripped off
  83. // during output so the %title's standard Drupal wrappers don't match.
  84. // $this->assertText(t('@type %title has been created.', $t_args));
  85. // Verify the node data saved correctly.
  86. $matches = array();
  87. if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
  88. $nid = end($matches);
  89. $node = node_load($nid);
  90. // Only the non-default values are stored.
  91. $expected = array(
  92. 'und' => array(
  93. 'dcterms.title' => array(
  94. 'value' => '[node:title] ponies',
  95. ),
  96. ),
  97. );
  98. $this->assertEqual($expected, $node->metatags);
  99. }
  100. else {
  101. $this->fail(t('Could not determine node ID for created node.'));
  102. }
  103. // Verify the title is using the custom default for this content type.
  104. $xpath = $this->xpath("//meta[@name='dcterms.title']");
  105. $this->assertEqual(count($xpath), 1);
  106. $this->assertEqual($xpath[0]['content'], "Who likes magic ponies");
  107. }
  108. }
  109. class MetaTagsUnitTest extends MetaTagsTestHelper {
  110. public static function getInfo() {
  111. return array(
  112. 'name' => 'Metatag unit tests',
  113. 'description' => 'Test basic meta tag functionality.',
  114. 'group' => 'Metatag',
  115. );
  116. }
  117. /**
  118. * Test the metatag_config_load_with_defaults() function.
  119. */
  120. public function testConfigLoadDefaults() {
  121. $defaults = metatag_config_load_with_defaults('test:foo');
  122. $this->assertEqual($defaults, array(
  123. // Basic meta tags.
  124. 'title' => array('value' => 'Test altered title'),
  125. 'description' => array('value' => 'Test foo description'),
  126. 'abstract' => array('value' => 'Test foo abstract'),
  127. // 'keywords' => array('value' => ''),
  128. // Advanced meta tags.
  129. // 'robots' => array('value' => ''),
  130. // 'news_keywords' => array('value' => ''),
  131. // 'standout' => array('value' => ''),
  132. // 'robots' => array('value' => ''),
  133. // 'standout' => array('value' => ''),
  134. 'generator' => array('value' => 'Drupal 7 (http://drupal.org)'),
  135. // 'standout' => array('value' => ''),
  136. // 'image_src' => array('value' => ''),
  137. 'canonical' => array('value' => '[current-page:url:absolute]'),
  138. 'shortlink' => array('value' => '[current-page:url:unaliased]'),
  139. // 'publisher' => array('value' => ''),
  140. // 'author' => array('value' => ''),
  141. // 'original-source' => array('value' => ''),
  142. // 'revisit-after' => array('value' => ''),
  143. // 'content-language' => array('value' => ''),
  144. // Fake meta tag.
  145. 'test:foo' => array('value' => 'foobar'),
  146. ));
  147. }
  148. public function testEntitySupport() {
  149. $test_cases[1] = array('type' => 'node', 'bundle' => 'article', 'expected' => TRUE);
  150. $test_cases[2] = array('type' => 'node', 'bundle' => 'page', 'expected' => TRUE);
  151. $test_cases[3] = array('type' => 'node', 'bundle' => 'invalid-bundle', 'expected' => FALSE);
  152. $test_cases[4] = array('type' => 'user', 'expected' => TRUE);
  153. $test_cases[5] = array('type' => 'taxonomy_term', 'bundle' => 'tags', 'expected' => TRUE);
  154. $test_cases[6] = array('type' => 'taxonomy_term', 'bundle' => 'invalid-bundle', 'expected' => FALSE);
  155. foreach ($test_cases as $test_case) {
  156. $test_case += array('bundle' => NULL);
  157. $this->assertMetatagEntityHasMetatags($test_case['type'], $test_case['bundle'], $test_case['expected']);
  158. }
  159. variable_set('metatag_test_entity_info_disable', TRUE);
  160. drupal_static_reset('metatag_entity_has_metatags');
  161. drupal_static_reset('metatag_entity_supports_metatags');
  162. entity_info_cache_clear();
  163. $test_cases[2]['expected'] = FALSE;
  164. $test_cases[4]['expected'] = FALSE;
  165. $test_cases[6]['expected'] = FALSE;
  166. foreach ($test_cases as $test_case) {
  167. $test_case += array('bundle' => NULL);
  168. $this->assertMetatagEntityHasMetatags($test_case['type'], $test_case['bundle'], $test_case['expected']);
  169. }
  170. }
  171. function assertMetatagEntityHasMetatags($entity_type, $bundle, $expected) {
  172. $entity = entity_create_stub_entity($entity_type, array(0, NULL, $bundle));
  173. return $this->assertEqual(
  174. metatag_entity_has_metatags($entity_type, $entity),
  175. $expected,
  176. t("metatag_entity_has_metatags(:type, :entity) is :expected", array(
  177. ':type' => var_export($entity_type, TRUE),
  178. ':entity' => var_export($entity, TRUE),
  179. ':expected' => var_export($expected, TRUE),
  180. ))
  181. );
  182. }
  183. /**
  184. * Test the metatag_config_instance_label() function.
  185. */
  186. public function testConfigLabels() {
  187. $test_cases = array(
  188. 'node' => 'Node',
  189. 'node:article' => 'Node: Article',
  190. 'node:article:c' => 'Node: Article: Unknown (c)',
  191. 'node:b' => 'Node: Unknown (b)',
  192. 'node:b:c' => 'Node: Unknown (b): Unknown (c)',
  193. 'a' => 'Unknown (a)',
  194. 'a:b' => 'Unknown (a): Unknown (b)',
  195. 'a:b:c' => 'Unknown (a): Unknown (b): Unknown (c)',
  196. 'a:b:c:d' => 'Unknown (a): Unknown (b): Unknown (c): Unknown (d)',
  197. );
  198. foreach ($test_cases as $input => $expected_output) {
  199. drupal_static_reset('metatag_config_instance_label');
  200. $actual_output = metatag_config_instance_label($input);
  201. $this->assertEqual($actual_output, $expected_output);
  202. }
  203. }
  204. }
  205. // TODO: Test each meta tag.
  206. // TODO: Scenarios.
  207. //
  208. // 1. Node
  209. // * No language assignment.
  210. // * First save.
  211. //
  212. // 2. Node
  213. // * No language assignment.
  214. // * Edit existing revision.
  215. //
  216. // 3. Node
  217. // * No language assignment.
  218. // * Create new revision.
  219. // * Publish new revision.
  220. //
  221. // 4. Node
  222. // * No language assignment.
  223. // * Create new revision.
  224. // * Delete new revision.
  225. //
  226. // 5. Node + Translation
  227. // * No language assignment
  228. // * Change language assignment.
  229. // * Edit existing revision.
  230. //
  231. // 6. Node + Translation
  232. // * No language assignment
  233. // * Change language assignment.
  234. // * Create new revision.
  235. // * Publish new revision.
  236. //
  237. // 7. Node + Translation
  238. // * No language assignment
  239. // * Change language assignment.
  240. // * Create new revision.
  241. // * Delete new revision.
  242. //
  243. // 8. Node + Translation
  244. // * Initial language assignment
  245. //
  246. // 9. Node + Translation
  247. // * Initial language assignment
  248. // * Create new revision.
  249. // * Publish new revision.
  250. //
  251. // 10. Node + Translation
  252. // * Initial language assignment
  253. // * Create new revision.
  254. // * Delete new revision.
  255. //
  256. // 11. Node + Translation
  257. // * Initial language assignment
  258. // * Change language assignment.
  259. // * Create new revision.
  260. // * Publish new revision.
  261. //
  262. // 12. Node + Translation
  263. // * Initial language assignment
  264. // * Change language assignment.
  265. // * Create new revision.
  266. // * Delete new revision.
  267. //
  268. // 13. Node + Translation
  269. // * Initial language assignment
  270. // * Create translated node.
  271. //
  272. // 14. Node + Translation
  273. // * Initial language assignment
  274. // * Create new revision.
  275. // * Publish new revision.
  276. // * Create translated node.
  277. //
  278. // 15. Node + Translation
  279. // * Initial language assignment
  280. // * Create new revision.
  281. // * Publish new revision.
  282. // * Create translated node.
  283. // * Delete translated node.
  284. //
  285. // 16. Node + Translation
  286. // * Initial language assignment
  287. // * Create translated node.
  288. // * Delete original node.
  289. //
  290. // 17. Node + Translation
  291. // * Initial language assignment
  292. // * Create new revision.
  293. // * Publish new revision.
  294. // * Create translated node.
  295. // * Delete original node.
  296. //
  297. // 18. Node + entity_translation
  298. // * Initial language assignment
  299. // * Create translated node.
  300. //
  301. // 19. Node + entity_translation
  302. // * Initial language assignment
  303. // * Create translated node.
  304. // * Delete original.
  305. //
  306. // 20. Node + entity_translation
  307. // * Initial language assignment
  308. // * Create translated node.
  309. // * Create new revision.
  310. // * Publish new revision.
  311. //
  312. // 21. Node + entity_translation
  313. // * Initial language assignment
  314. // * Create translated node.
  315. // * Create new revision.
  316. // * Publish new revision.
  317. // * Delete new revision.
  318. //
  319. // 22. Node + entity_translation
  320. // * Initial language assignment
  321. // * Create translated node.
  322. // * Create new revision.
  323. // * Publish new revision.
  324. // * Delete original.
  325. //
  326. // 23. Node + entity_translation
  327. // * Initial language assignment
  328. // * Create translated node.
  329. // * Create new revision.
  330. // * Publish new revision.
  331. // * Delete original.
  332. //
  333. // 24. Node + entity_translation
  334. // * Initial language assignment
  335. // * Create new revision.
  336. // * Publish new revision.
  337. // * Create translated node.
  338. //
  339. // 25. Node + entity_translation
  340. // * Initial language assignment
  341. // * Create new revision.
  342. // * Publish new revision.
  343. // * Create translated node.
  344. // * Delete new revision.
  345. //
  346. //
  347. // 30. Node + i18n
  348. //
  349. //
  350. // 50. Term
  351. // * Create term.
  352. //
  353. // 51. Term
  354. // * Create term.
  355. // * Change values.
  356. //
  357. //
  358. // 60. User
  359. // * Create user.
  360. //
  361. // 61. User
  362. // * Create user.
  363. // * Change values.
  364. //
  365. //
  366. // 70. Custom path
  367. // * Defaults loaded.