metatag.unit.test 4.5 KB

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