uc_file.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @file
  4. * File download product feature tests.
  5. */
  6. /**
  7. * Tests the file download purchase functionality.
  8. */
  9. class UbercartFileTestCase extends UbercartTestHelper {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'File downloads',
  13. 'description' => 'Ensures that the purchase of file downloads functions correctly.',
  14. 'group' => 'Ubercart',
  15. );
  16. }
  17. /**
  18. * Overrides DrupalWebTestCase::setUp().
  19. */
  20. public function setUp($modules = array(), $permissions = array()) {
  21. $modules = array('uc_payment', 'uc_payment_pack', 'uc_file');
  22. $permissions = array();
  23. parent::setUp($modules, $permissions);
  24. // Need admin permissions in order to change file download settings.
  25. $this->drupalLogin($this->adminUser);
  26. // Set up directory for files to live in.
  27. $this->configureDownloadDirectory();
  28. }
  29. public function testFilePurchaseCheckout() {
  30. // Add file download feature to the test product.
  31. $filename = $this->uploadTestFile();
  32. $this->drupalLogin($this->adminUser);
  33. $this->drupalPost('node/' . $this->product->nid . '/edit/features', array('feature' => 'file'), t('Add'));
  34. $edit = array(
  35. 'uc_file_model' => '',
  36. 'uc_file_filename' => $filename,
  37. );
  38. $this->drupalPost(NULL, $edit, t('Save feature'));
  39. // Check out with the test product.
  40. $this->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
  41. $order = $this->checkout();
  42. uc_payment_enter($order->order_id, 'other', $order->order_total);
  43. // Test that the file was granted.
  44. $this->drupalGet('user/' . $order->uid . '/purchased-files');
  45. $this->assertText($filename, 'File found in list of purchased files.');
  46. // Test that the email is correct.
  47. $mail = $this->findMail('/File Downloads for Order# ' . preg_quote($order->order_id) . '/');
  48. // Delete the user.
  49. user_delete($order->uid);
  50. // Run cron to ensure deleted users are handled correctly.
  51. $this->drupalLogout();
  52. $this->cronRun();
  53. }
  54. /**
  55. * Helper function to configure Credit Card payment method settings.
  56. */
  57. protected function configureDownloadDirectory() {
  58. // Create directory for downloads, make it readable and writeable.
  59. // Putting this under sites/default/files because SimpleTest needs to be
  60. // able to create the directory - this is NOT where you'd put the downloads
  61. // directory on a live site. On a live site, it should be outside the web root.
  62. drupal_mkdir('sites/default/files/file-downloads', 0755);
  63. $this->drupalPost(
  64. 'admin/store/settings/products',
  65. array(
  66. 'uc_file_base_dir' => 'sites/default/files/file-downloads',
  67. ),
  68. t('Save configuration')
  69. );
  70. $this->assertFieldByName(
  71. 'uc_file_base_dir',
  72. 'sites/default/files/file-downloads',
  73. 'Download file path has been set.'
  74. );
  75. }
  76. /**
  77. * Helper function to upload test file for downloading.
  78. */
  79. protected function uploadTestFile() {
  80. $filename = 'README.txt';
  81. // Use the Ubercart README.txt because we know it will always be there
  82. // and we know in advance how big it is.
  83. copy(drupal_get_path('module', 'uc_file') . '/../' . $filename,
  84. 'sites/default/files/file-downloads/README.txt'
  85. );
  86. return $filename;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. protected function tearDown() {
  92. // Cleanup file download directory after test.
  93. drupal_unlink('sites/default/files/file-downloads/README.txt');
  94. drupal_rmdir('sites/default/files/file-downloads');
  95. parent::tearDown();
  96. }
  97. }