synonyms_provider_property.test 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Test cases for Property synonyms provider module.
  5. */
  6. /**
  7. * Test PropertySynonymsBehavior class.
  8. */
  9. class SynonymsProviderPropertyWebTestCase extends SynonymsWebTestCase {
  10. /**
  11. * GetInfo method.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'PropertySynonymsBehavior',
  16. 'description' => 'Ensure that the synonyms module extracts synonyms from entity properties correctly.',
  17. 'group' => 'Synonyms',
  18. );
  19. }
  20. public function setUp($modules = array()) {
  21. $this->behavior_implementation['behavior'] = 'autocomplete';
  22. $this->behavior_implementation['provider'] = 'whatever';
  23. array_unshift($modules, 'synonyms_provider_property');
  24. parent::setUp($modules);
  25. // We can't initialize this before parent::setUp() because we depend on
  26. // synonyms_provider_property_provider_name() function.
  27. synonyms_behavior_implementation_delete($this->behavior_implementation);
  28. $this->behavior_implementation['provider'] = synonyms_provider_property_provider_name('tid');
  29. synonyms_behavior_implementation_save($this->behavior_implementation);
  30. foreach (synonyms_behavior_get($this->behavior_implementation['behavior'], $this->behavior_implementation['entity_type'], $this->behavior_implementation['bundle'], TRUE) as $behavior_implementation) {
  31. if ($behavior_implementation['provider'] == $this->behavior_implementation['provider']) {
  32. $this->behavior_implementation = $behavior_implementation;
  33. break;
  34. }
  35. }
  36. }
  37. /**
  38. * Test property-based synonyms provider.
  39. */
  40. public function testProperty() {
  41. $term = (object) array(
  42. 'vid' => $this->vocabulary->vid,
  43. 'name' => $this->randomName(),
  44. );
  45. taxonomy_term_save($term);
  46. $term2 = (object) array(
  47. 'vid' => $this->vocabulary->vid,
  48. 'name' => $this->randomName(),
  49. );
  50. taxonomy_term_save($term2);
  51. // Test extractSynonyms() method.
  52. $synonyms = $this->behavior_implementation['object']->extractSynonyms($term);
  53. $this->assertIdentical($synonyms, array($term->tid), $this->behavior_implementation['class'] . '::extractSynonyms() passed.');
  54. // Test synonymsFind() method.
  55. $condition = db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, 0);
  56. $this->assertSynonymsFind($condition, array(), 'on non-existing synonym');
  57. $condition = db_and()->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $term->tid);
  58. $this->assertSynonymsFind($condition, array(array('entity_id' => $term->tid, 'synonym' => $term->tid)), 'on a synonym');
  59. $condition = db_and()
  60. ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $term->tid)
  61. ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $term2->tid);
  62. $this->assertSynonymsFind($condition, array(), 'on a synonym AND on another synonym');
  63. $condition = db_or()
  64. ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $term->tid)
  65. ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $term2->tid);
  66. $this->assertSynonymsFind($condition, array(array('entity_id' => $term->tid, 'synonym' => $term->tid), array('entity_id' => $term2->tid, 'synonym' => $term2->tid)), 'on a synonym OR another synonym');
  67. }
  68. /**
  69. * Supportive assert method.
  70. *
  71. * Assert results of synonymsFind() method.
  72. *
  73. * @param QueryConditionInterface $condition
  74. * Query condition to be passed on into synonymsFind() method
  75. * @param array $standard
  76. * Expected result
  77. * @param string $message
  78. * Optional additional assert message
  79. */
  80. protected function assertSynonymsFind(QueryConditionInterface $condition, $standard, $message = '') {
  81. $map_standard = array();
  82. foreach ($standard as $v) {
  83. $map_standard[$v['entity_id']] = $v['synonym'];
  84. }
  85. $message = $this->behavior_implementation['class'] . '::synonymsFind() ' . $message;
  86. $result = $this->behavior_implementation['object']->synonymsFind($condition);
  87. $count = 0;
  88. foreach ($result as $row) {
  89. $count++;
  90. if (!isset($map_standard[$row->entity_id]) || $map_standard[$row->entity_id] != $row->synonym) {
  91. $this->fail($message);
  92. return;
  93. }
  94. }
  95. $this->assertEqual(count($map_standard), $count, $message);
  96. }
  97. }