metatag.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 MetaTagsUnitTest extends MetaTagsTestHelper {
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'Meta tag unit tests',
  15. 'description' => 'Test basic meta tag functionality.',
  16. 'group' => 'Meta tags',
  17. );
  18. }
  19. /**
  20. * Test the metatag_config_load_with_defaults() function.
  21. */
  22. public function testConfigLoadDefaults() {
  23. $defaults = metatag_config_load_with_defaults('test:foo');
  24. $this->assertEqual($defaults, array(
  25. 'description' => array('value' => 'Test foo description'),
  26. 'abstract' => array('value' => 'Test foo abstract'),
  27. 'title' => array('value' => 'Test altered title'),
  28. 'test:foo' => array('value' => 'foobar'),
  29. 'generator' => array('value' => 'Drupal 7 (http://drupal.org)'),
  30. 'canonical' => array('value' => '[current-page:url:absolute]'),
  31. 'shortlink' => array('value' => '[current-page:url:unaliased]'),
  32. ));
  33. }
  34. public function testEntitySupport() {
  35. $test_cases[1] = array('type' => 'node', 'bundle' => 'article', 'expected' => TRUE);
  36. $test_cases[2] = array('type' => 'node', 'bundle' => 'page', 'expected' => TRUE);
  37. $test_cases[3] = array('type' => 'node', 'bundle' => 'invalid-bundle', 'expected' => FALSE);
  38. $test_cases[4] = array('type' => 'user', 'expected' => TRUE);
  39. foreach ($test_cases as $test_case) {
  40. $test_case += array('bundle' => NULL);
  41. $this->assertMetatagEntityHasMetatags($test_case['type'], $test_case['bundle'], $test_case['expected']);
  42. }
  43. variable_set('metatag_test_entity_info_disable', TRUE);
  44. drupal_static_reset('metatag_entity_has_metatags');
  45. drupal_static_reset('metatag_entity_supports_metatags');
  46. entity_info_cache_clear();
  47. $test_cases[2]['expected'] = FALSE;
  48. $test_cases[4]['expected'] = FALSE;
  49. foreach ($test_cases as $test_case) {
  50. $test_case += array('bundle' => NULL);
  51. $this->assertMetatagEntityHasMetatags($test_case['type'], $test_case['bundle'], $test_case['expected']);
  52. }
  53. }
  54. function assertMetatagEntityHasMetatags($entity_type, $bundle, $expected) {
  55. $entity = entity_create_stub_entity($entity_type, array(0, NULL, $bundle));
  56. return $this->assertEqual(
  57. metatag_entity_has_metatags($entity_type, $entity),
  58. $expected,
  59. t("metatag_entity_has_metatags(:type, :entity) is :expected", array(
  60. ':type' => var_export($entity_type, TRUE),
  61. ':entity' => var_export($entity, TRUE),
  62. ':expected' => var_export($expected, TRUE),
  63. ))
  64. );
  65. }
  66. /**
  67. * Test the metatag_config_instance_label() function.
  68. */
  69. public function testConfigLabels() {
  70. $test_cases = array(
  71. 'node' => 'Node',
  72. 'node:article' => 'Node: Article',
  73. 'node:article:c' => 'Node: Article: Unknown (c)',
  74. 'node:b' => 'Node: Unknown (b)',
  75. 'node:b:c' => 'Node: Unknown (b): Unknown (c)',
  76. 'a' => 'Unknown (a)',
  77. 'a:b' => 'Unknown (a): Unknown (b)',
  78. 'a:b:c' => 'Unknown (a): Unknown (b): Unknown (c)',
  79. 'a:b:c:d' => 'Unknown (a): Unknown (b): Unknown (c): Unknown (d)',
  80. );
  81. foreach ($test_cases as $input => $expected_output) {
  82. drupal_static_reset('metatag_config_instance_label');
  83. $actual_output = metatag_config_instance_label($input);
  84. $this->assertEqual($actual_output, $expected_output);
  85. }
  86. }
  87. }