metatag.unit.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Unit tests for the Metatag module.
  4. *
  5. * @todo These aren't really unit tests, we might need to fix that.
  6. */
  7. class MetatagCoreUnitTest extends MetatagTestHelper {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Metatag unit tests',
  14. 'description' => 'Test basic meta tag functionality for entities.',
  15. 'group' => 'Metatag',
  16. 'dependencies' => array('ctools', 'token'),
  17. );
  18. }
  19. /**
  20. * Test the metatag_config_load_with_defaults() function.
  21. */
  22. public function testConfigLoadDefaults() {
  23. // Load the global defaults, inc the fake entity.
  24. $defaults = metatag_config_load_with_defaults('test:foo');
  25. // Load the example values and verify they're the same as the globals.
  26. $extra_tags = array(
  27. // Fake meta tag.
  28. 'test:foo' => array('value' => 'foobar'),
  29. );
  30. $new_values = array_merge($extra_tags, $this->getTestDefaults());
  31. // Confirm that the values are equal.
  32. $this->assertEqual($defaults, $new_values);
  33. }
  34. /**
  35. * Test the basic entity handling.
  36. */
  37. public function testEntitySupport() {
  38. $test_cases[1] = array('type' => 'node', 'bundle' => 'article', 'expected' => TRUE);
  39. $test_cases[2] = array('type' => 'node', 'bundle' => 'page', 'expected' => TRUE);
  40. $test_cases[3] = array('type' => 'node', 'bundle' => 'invalid-bundle', 'expected' => FALSE);
  41. $test_cases[4] = array('type' => 'user', 'expected' => TRUE);
  42. $test_cases[5] = array('type' => 'taxonomy_term', 'bundle' => 'tags', 'expected' => TRUE);
  43. $test_cases[6] = array('type' => 'taxonomy_term', 'bundle' => 'invalid-bundle', 'expected' => FALSE);
  44. foreach ($test_cases as $test_case) {
  45. $test_case += array('bundle' => NULL);
  46. $this->assertMetatagEntitySupportsMetatags($test_case['type'], $test_case['bundle'], $test_case['expected']);
  47. }
  48. // Disable meta tags for these.
  49. metatag_entity_type_disable('node', 'page');
  50. metatag_entity_type_disable('user');
  51. $test_cases[2]['expected'] = FALSE;
  52. $test_cases[4]['expected'] = FALSE;
  53. $test_cases[6]['expected'] = FALSE;
  54. foreach ($test_cases as $test_case) {
  55. $test_case += array('bundle' => NULL);
  56. $this->assertMetatagEntitySupportsMetatags($test_case['type'], $test_case['bundle'], $test_case['expected']);
  57. }
  58. }
  59. /**
  60. * Confirm an entity supports meta tags.
  61. *
  62. * @param string $entity_type
  63. * The name of the entity type to test.
  64. * @param string $bundle
  65. * The name of the entity bundle to test.
  66. * @param array $expected
  67. * The expected results.
  68. *
  69. * @return object
  70. * An assertion.
  71. */
  72. function assertMetatagEntitySupportsMetatags($entity_type, $bundle, $expected) {
  73. $entity = entity_create_stub_entity($entity_type, array(0, NULL, $bundle));
  74. return $this->assertEqual(
  75. metatag_entity_supports_metatags($entity_type, $bundle),
  76. $expected,
  77. t("metatag_entity_supports_metatags(:type, :bundle) is :expected", array(
  78. ':type' => var_export($entity_type, TRUE),
  79. ':bundle' => var_export($bundle, TRUE),
  80. ':expected' => var_export($expected, TRUE),
  81. ))
  82. );
  83. }
  84. /**
  85. * Test the metatag_config_instance_label() function.
  86. */
  87. public function testConfigLabels() {
  88. $test_cases = array(
  89. 'node' => 'Node',
  90. 'node:article' => 'Node: Article',
  91. 'node:article:c' => 'Node: Article: Unknown (c)',
  92. 'node:b' => 'Node: Unknown (b)',
  93. 'node:b:c' => 'Node: Unknown (b): Unknown (c)',
  94. 'a' => 'Unknown (a)',
  95. 'a:b' => 'Unknown (a): Unknown (b)',
  96. 'a:b:c' => 'Unknown (a): Unknown (b): Unknown (c)',
  97. 'a:b:c:d' => 'Unknown (a): Unknown (b): Unknown (c): Unknown (d)',
  98. );
  99. foreach ($test_cases as $input => $expected_output) {
  100. drupal_static_reset('metatag_config_instance_label');
  101. $actual_output = metatag_config_instance_label($input);
  102. $this->assertEqual($actual_output, $expected_output);
  103. }
  104. }
  105. /**
  106. * Test the _metatag_config_instance_sort() function.
  107. */
  108. public function testConfigInstanceSort() {
  109. $input = array(
  110. 'node:article',
  111. 'global:frontpage',
  112. 'file',
  113. 'node:page',
  114. 'global',
  115. 'node',
  116. 'global:404',
  117. );
  118. usort($input, '_metatag_config_instance_sort');
  119. $this->assertIdentical($input, array(
  120. 'global',
  121. 'global:404',
  122. 'global:frontpage',
  123. 'file',
  124. 'node',
  125. 'node:article',
  126. 'node:page',
  127. ));
  128. }
  129. }