PathUnitTestCase.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Bugfixes made over time test class.
  4. */
  5. abstract class Redis_Tests_Path_PathUnitTestCase extends Redis_Tests_AbstractUnitTestCase
  6. {
  7. /**
  8. * @var Cache bin identifier
  9. */
  10. static private $id = 1;
  11. /**
  12. * Get cache backend
  13. *
  14. * @return Redis_Path_HashLookupInterface
  15. */
  16. final protected function getBackend($name = null)
  17. {
  18. if (null === $name) {
  19. // This is needed to avoid conflict between tests, each test
  20. // seems to use the same Redis namespace and conflicts are
  21. // possible.
  22. $name = 'cache' . (self::$id++);
  23. }
  24. $className = Redis_Client::getClass(Redis_Client::REDIS_IMPL_PATH);
  25. $hashLookup = new $className(Redis_Client::getClient(), 'path', Redis_Client::getDefaultPrefix('path'));
  26. return $hashLookup;
  27. }
  28. /**
  29. * Tests basic functionnality
  30. */
  31. public function testPathLookup()
  32. {
  33. $backend = $this->getBackend();
  34. $source = $backend->lookupSource('node-1-fr', 'fr');
  35. $this->assertIdentical(null, $source);
  36. $alias = $backend->lookupAlias('node/1', 'fr');
  37. $this->assertIdentical(null, $source);
  38. $backend->saveAlias('node/1', 'node-1-fr', 'fr');
  39. $source = $backend->lookupSource('node-1-fr', 'fr');
  40. $source = $backend->lookupSource('node-1-fr', 'fr');
  41. $this->assertIdentical('node/1', $source);
  42. $alias = $backend->lookupAlias('node/1', 'fr');
  43. $this->assertIdentical('node-1-fr', $alias);
  44. // Delete and ensure it does not exist anymore.
  45. $backend->deleteAlias('node/1', 'node-1-fr', 'fr');
  46. $source = $backend->lookupSource('node-1-fr', 'fr');
  47. $this->assertIdentical(null, $source);
  48. $alias = $backend->lookupAlias('node/1', 'fr');
  49. $this->assertIdentical(null, $source);
  50. // Set more than one aliases and ensure order at loading.
  51. $backend->saveAlias('node/1', 'node-1-fr-1', 'fr');
  52. $backend->saveAlias('node/1', 'node-1-fr-2', 'fr');
  53. $backend->saveAlias('node/1', 'node-1-fr-3', 'fr');
  54. $alias = $backend->lookupAlias('node/1', 'fr');
  55. $this->assertIdentical('node-1-fr-3', $alias);
  56. // Add another alias to test the delete language feature.
  57. // Also add some other languages aliases.
  58. $backend->saveAlias('node/1', 'node-1');
  59. $backend->saveAlias('node/2', 'node-2-en', 'en');
  60. $backend->saveAlias('node/3', 'node-3-ca', 'ca');
  61. // Ok, delete fr and tests every other are still there.
  62. $backend->deleteLanguage('fr');
  63. $alias = $backend->lookupAlias('node/1');
  64. $this->assertIdentical('node-1', $alias);
  65. $alias = $backend->lookupAlias('node/2', 'en');
  66. $this->assertIdentical('node-2-en', $alias);
  67. $alias = $backend->lookupAlias('node/3', 'ca');
  68. $this->assertIdentical('node-3-ca', $alias);
  69. // Now create back a few entries in some langage and
  70. // ensure fallback to no language also works.
  71. $backend->saveAlias('node/4', 'node-4');
  72. $backend->saveAlias('node/4', 'node-4-es', 'es');
  73. $alias = $backend->lookupAlias('node/4');
  74. $this->assertIdentical('node-4', $alias);
  75. $alias = $backend->lookupAlias('node/4', 'es');
  76. $this->assertIdentical('node-4-es', $alias);
  77. $alias = $backend->lookupAlias('node/4', 'fr');
  78. $this->assertIdentical('node-4', $alias);
  79. }
  80. /**
  81. * Tests https://www.drupal.org/node/2728831
  82. */
  83. public function testSomeEdgeCaseFalseNegative()
  84. {
  85. $backend = $this->getBackend();
  86. $backend->deleteLanguage('fr');
  87. $backend->deleteLanguage('und');
  88. $backend->saveAlias('node/123', 'node-123');
  89. // Language lookup should return the language neutral value if no value
  90. $source = $backend->lookupSource('node-123', 'fr');
  91. $this->assertIdentical($source, 'node/123');
  92. $source = $backend->lookupAlias('node/123', 'fr');
  93. $this->assertIdentical($source, 'node-123');
  94. // Now, let's consider we have an item we don't know if it exists or
  95. // not, per definition we should not return a strict FALSE but a NULL
  96. // value instead to tell "we don't know anything about this". In a
  97. // very specific use-case, if the language neutral value is a strict
  98. // "not exists" value, it should still return NULL instead of FALSE
  99. // if another language was asked for.
  100. // Store "value null" for the language neutral entry
  101. $backend->saveAlias('node/456', Redis_Path_HashLookupInterface::VALUE_NULL);
  102. $source = $backend->lookupAlias('node/456');
  103. $this->assertIdentical(false, $source);
  104. $source = $backend->lookupAlias('node/456', 'fr');
  105. $this->assertIdentical(null, $source);
  106. }
  107. /**
  108. * Tests that lookup is case insensitive
  109. */
  110. public function testCaseInsensitivePathLookup()
  111. {
  112. $backend = $this->getBackend();
  113. $backend->saveAlias('node/1', 'Node-1-FR', 'fr');
  114. $source = $backend->lookupSource('NODE-1-fr', 'fr');
  115. $this->assertIdentical('node/1', $source);
  116. $source = $backend->lookupSource('node-1-FR', 'fr');
  117. $this->assertIdentical('node/1', $source);
  118. $alias = $backend->lookupAlias('node/1', 'fr');
  119. $this->assertIdentical('node-1-fr', strtolower($alias));
  120. // Delete and ensure it does not exist anymore.
  121. $backend->deleteAlias('node/1', 'node-1-FR', 'fr');
  122. $source = $backend->lookupSource('Node-1-FR', 'fr');
  123. $this->assertIdentical(null, $source);
  124. $alias = $backend->lookupAlias('node/1', 'fr');
  125. $this->assertIdentical(null, $source);
  126. }
  127. }