registry.test 4.8 KB

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