file_example.test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @file
  4. * Tests for File Example.
  5. */
  6. /**
  7. * Functional tests for the File Example module.
  8. *
  9. * @ingroup file_example
  10. */
  11. class FileExampleTest extends DrupalWebTestCase {
  12. protected $priviledgedUser;
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public static function getInfo() {
  17. return array(
  18. 'name' => 'File Example Functionality',
  19. 'description' => 'Test File Example features and sample streamwrapper.',
  20. 'group' => 'Examples',
  21. );
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function setUp() {
  27. parent::setUp(array('file_example'));
  28. $this->priviledgedUser = $this->drupalCreateUser(array('use file example'));
  29. $this->drupalLogin($this->priviledgedUser);
  30. }
  31. /**
  32. * Test the basic File Example UI.
  33. *
  34. * - Create a directory to work with
  35. * - Foreach scheme create and read files using each of the three methods.
  36. */
  37. public function testFileExampleBasic() {
  38. $expected_text = array(
  39. t('Write managed file') => t('Saved managed file'),
  40. t('Write unmanaged file') => t('Saved file as'),
  41. t('Unmanaged using PHP') => t('Saved file as'),
  42. );
  43. // For each of the three buttons == three write types.
  44. $buttons = array(
  45. t('Write managed file'),
  46. t('Write unmanaged file'),
  47. t('Unmanaged using PHP'),
  48. );
  49. foreach ($buttons as $button) {
  50. // For each scheme supported by Drupal + the session:// wrapper.
  51. $schemes = array('public', 'private', 'temporary', 'session');
  52. foreach ($schemes as $scheme) {
  53. // Create a directory for use.
  54. $dirname = $scheme . '://' . $this->randomName(10);
  55. // Directory does not yet exist; assert that.
  56. $edit = array(
  57. 'directory_name' => $dirname,
  58. );
  59. $this->drupalPost('examples/file_example/fileapi', $edit, t('Check to see if directory exists'));
  60. $this->assertRaw(t('Directory %dirname does not exist', array('%dirname' => $dirname)), 'Verify that directory does not exist.');
  61. $this->drupalPost('examples/file_example/fileapi', $edit, t('Create directory'));
  62. $this->assertRaw(t('Directory %dirname is ready for use', array('%dirname' => $dirname)));
  63. $this->drupalPost('examples/file_example/fileapi', $edit, t('Check to see if directory exists'));
  64. $this->assertRaw(t('Directory %dirname exists', array('%dirname' => $dirname)), 'Verify that directory now does exist.');
  65. // Create a file in the directory we created.
  66. $content = $this->randomName(30);
  67. $filename = $dirname . '/' . $this->randomName(30) . '.txt';
  68. // Assert that the file we're about to create does not yet exist.
  69. $edit = array(
  70. 'fileops_file' => $filename,
  71. );
  72. $this->drupalPost('examples/file_example/fileapi', $edit, t('Check to see if file exists'));
  73. $this->assertRaw(t('The file %filename does not exist', array('%filename' => $filename)), 'Verify that file does not yet exist.');
  74. debug(
  75. t('Processing button=%button, scheme=%scheme, dir=%dirname, file=%filename',
  76. array(
  77. '%button' => $button,
  78. '%scheme' => $scheme,
  79. '%filename' => $filename,
  80. '%dirname' => $dirname,
  81. )
  82. )
  83. );
  84. $edit = array(
  85. 'write_contents' => $content,
  86. 'destination' => $filename,
  87. );
  88. $this->drupalPost('examples/file_example/fileapi', $edit, $button);
  89. $this->assertText($expected_text[$button]);
  90. // Capture the name of the output file, as it might have changed due
  91. // to file renaming.
  92. $element = $this->xpath('//span[@id="uri"]');
  93. $output_filename = (string) $element[0];
  94. debug($output_filename, 'Name of output file');
  95. // Click the link provided that is an easy way to get the data for
  96. // checking and make sure that the data we put in is what we get out.
  97. if (!in_array($scheme, array('private', 'temporary'))) {
  98. $this->clickLink(t('this URL'));
  99. $this->assertText($content);
  100. }
  101. // Verify that the file exists.
  102. $edit = array(
  103. 'fileops_file' => $filename,
  104. );
  105. $this->drupalPost('examples/file_example/fileapi', $edit, t('Check to see if file exists'));
  106. $this->assertRaw(t('The file %filename exists', array('%filename' => $filename)), 'Verify that file now exists.');
  107. // Now read the file that got written above and verify that we can use
  108. // the writing tools.
  109. $edit = array(
  110. 'fileops_file' => $output_filename,
  111. );
  112. $this->drupalPost('examples/file_example/fileapi', $edit, t('Read the file and store it locally'));
  113. $this->assertText(t('The file was read and copied'));
  114. $edit = array(
  115. 'fileops_file' => $filename,
  116. );
  117. $this->drupalPost('examples/file_example/fileapi', $edit, t('Delete file'));
  118. $this->assertText(t('Successfully deleted'));
  119. $this->drupalPost('examples/file_example/fileapi', $edit, t('Check to see if file exists'));
  120. $this->assertRaw(t('The file %filename does not exist', array('%filename' => $filename)), 'Verify file has been deleted.');
  121. $edit = array(
  122. 'directory_name' => $dirname,
  123. );
  124. $this->drupalPost('examples/file_example/fileapi', $edit, t('Delete directory'));
  125. $this->drupalPost('examples/file_example/fileapi', $edit, t('Check to see if directory exists'));
  126. $this->assertRaw(t('Directory %dirname does not exist', array('%dirname' => $dirname)), 'Verify that directory does not exist after deletion.');
  127. }
  128. }
  129. }
  130. }