registry.test

File

drupal/modules/simpletest/tests/registry.test
View source
  1. <?php
  2. class RegistryParseFileTestCase extends DrupalWebTestCase {
  3. public static function getInfo() {
  4. return array(
  5. 'name' => 'Registry parse file test',
  6. 'description' => 'Parse a simple file and check that its resources are saved to the database.',
  7. 'group' => 'System'
  8. );
  9. }
  10. function setUp() {
  11. $chrs = hash('sha256', microtime() . mt_rand());
  12. $this->fileName = 'registry_test_' . substr($chrs, 0, 16);
  13. $this->className = 'registry_test_class' . substr($chrs, 16, 16);
  14. $this->interfaceName = 'registry_test_interface' . substr($chrs, 32, 16);
  15. parent::setUp();
  16. }
  17. /**
  18. * testRegistryParseFile
  19. */
  20. function testRegistryParseFile() {
  21. _registry_parse_file($this->fileName, $this->getFileContents());
  22. foreach (array('className', 'interfaceName') as $resource) {
  23. $foundName = db_query('SELECT name FROM {registry} WHERE name = :name', array(':name' => $this->$resource))->fetchField();
  24. $this->assertTrue($this->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$resource)));
  25. }
  26. }
  27. /**
  28. * getFileContents
  29. */
  30. function getFileContents() {
  31. $file_contents = <<<CONTENTS
  32. <?php
  33. class {$this->className} {}
  34. interface {$this->interfaceName} {}
  35. CONTENTS;
  36. return $file_contents;
  37. }
  38. }
  39. class RegistryParseFilesTestCase extends DrupalWebTestCase {
  40. protected $fileTypes = array('new', 'existing_changed');
  41. public static function getInfo() {
  42. return array(
  43. 'name' => 'Registry parse files test',
  44. 'description' => 'Read two a simple files from disc, and check that their resources are saved to the database.',
  45. 'group' => 'System'
  46. );
  47. }
  48. function setUp() {
  49. parent::setUp();
  50. // Create files with some php to parse - one 'new', one 'existing' so
  51. // we test all the important code paths in _registry_parse_files.
  52. foreach ($this->fileTypes as $fileType) {
  53. $chrs = hash('sha256', microtime() . mt_rand());
  54. $this->$fileType = new stdClass();
  55. $this->$fileType->fileName = 'public://registry_test_' . substr($chrs, 0, 16);
  56. $this->$fileType->className = 'registry_test_class' . substr($chrs, 16, 16);
  57. $this->$fileType->interfaceName = 'registry_test_interface' . substr($chrs, 32, 16);
  58. $this->$fileType->contents = $this->getFileContents($fileType);
  59. file_save_data($this->$fileType->contents, $this->$fileType->fileName);
  60. if ($fileType == 'existing_changed') {
  61. // Add a record with an incorrect hash.
  62. $this->$fileType->fakeHash = hash('sha256', mt_rand());
  63. db_insert('registry_file')
  64. ->fields(array(
  65. 'hash' => $this->$fileType->fakeHash,
  66. 'filename' => $this->$fileType->fileName,
  67. ))
  68. ->execute();
  69. // Insert some fake resource records.
  70. foreach (array('class', 'interface') as $type) {
  71. db_insert('registry')
  72. ->fields(array(
  73. 'name' => $type . hash('sha256', microtime() . mt_rand()),
  74. 'type' => $type,
  75. 'filename' => $this->$fileType->fileName,
  76. ))
  77. ->execute();
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * testRegistryParseFiles
  84. */
  85. function testRegistryParseFiles() {
  86. _registry_parse_files($this->getFiles());
  87. foreach ($this->fileTypes as $fileType) {
  88. // Test that we have all the right resources.
  89. foreach (array('className', 'interfaceName') as $resource) {
  90. $foundName = db_query('SELECT name FROM {registry} WHERE name = :name', array(':name' => $this->$fileType->$resource))->fetchField();
  91. $this->assertTrue($this->$fileType->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$fileType->$resource)));
  92. }
  93. // Test that we have the right hash.
  94. $hash = db_query('SELECT hash FROM {registry_file} WHERE filename = :filename', array(':filename' => $this->$fileType->fileName))->fetchField();
  95. $this->assertTrue(hash('sha256', $this->$fileType->contents) == $hash, t('sha-256 for "@filename" matched.' . $fileType . $hash, array('@filename' => $this->$fileType->fileName)));
  96. }
  97. }
  98. /**
  99. * getFiles
  100. */
  101. function getFiles() {
  102. $files = array();
  103. foreach ($this->fileTypes as $fileType) {
  104. $files[$this->$fileType->fileName] = array('module' => '', 'weight' => 0);
  105. if ($fileType == 'existing_changed') {
  106. $files[$this->$fileType->fileName]['hash'] = $this->$fileType->fakeHash;
  107. }
  108. }
  109. return $files;
  110. }
  111. /**
  112. * getFileContents
  113. */
  114. function getFileContents($fileType) {
  115. $file_contents = <<<CONTENTS
  116. <?php
  117. class {$this->$fileType->className} {}
  118. interface {$this->$fileType->interfaceName} {}
  119. CONTENTS;
  120. return $file_contents;
  121. }
  122. }

Classes