file_example.test

  1. examples
    1. 7 file_example/file_example.test
    2. 8 file_example/file_example.test

Tests for File Example.

Classes

NameDescription
FileExampleTest

File

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