Commit e53131dd authored by alexpott's avatar alexpott

Issue #2394419 by hussainweb, cilefen, sachinwable: Clean-up file module test...

Issue #2394419 by hussainweb, cilefen, sachinwable: Clean-up file module test members - ensure property definition and use of camelCase naming convention
parent 505e3939
...@@ -57,7 +57,7 @@ function testUploadPath() { ...@@ -57,7 +57,7 @@ function testUploadPath() {
$node_file = file_load($node->{$field_name}->target_id); $node_file = file_load($node->{$field_name}->target_id);
// Do token replacement using the same user which uploaded the file, not // Do token replacement using the same user which uploaded the file, not
// the user running the test case. // the user running the test case.
$data = array('user' => $this->admin_user); $data = array('user' => $this->adminUser);
$subdirectory = \Drupal::token()->replace('[user:uid]/[user:name]', $data); $subdirectory = \Drupal::token()->replace('[user:uid]/[user:name]', $data);
$this->assertPathMatch('public://' . $subdirectory . '/' . $test_file->getFilename(), $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path with token replacements.', array('%file' => $node_file->getFileUri()))); $this->assertPathMatch('public://' . $subdirectory . '/' . $test_file->getFilename(), $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path with token replacements.', array('%file' => $node_file->getFileUri())));
} }
......
...@@ -24,12 +24,17 @@ ...@@ -24,12 +24,17 @@
*/ */
public static $modules = array('node', 'file', 'file_module_test', 'field_ui'); public static $modules = array('node', 'file', 'file_module_test', 'field_ui');
protected $admin_user; /**
* An user with administration permissions.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer node fields', 'administer node display', 'administer nodes', 'bypass node access')); $this->adminUser = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer node fields', 'administer node display', 'administer nodes', 'bypass node access'));
$this->drupalLogin($this->admin_user); $this->drupalLogin($this->adminUser);
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
} }
......
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,6 @@
* @group file * @group file
*/ */
class FileFieldValidateTest extends FileFieldTestBase { class FileFieldValidateTest extends FileFieldTestBase {
protected $field;
protected $node_type;
/** /**
* Tests the required property on file fields. * Tests the required property on file fields.
......
...@@ -222,7 +222,7 @@ function testMultiValuedWidget() { ...@@ -222,7 +222,7 @@ function testMultiValuedWidget() {
function testPrivateFileSetting() { function testPrivateFileSetting() {
$node_storage = $this->container->get('entity.manager')->getStorage('node'); $node_storage = $this->container->get('entity.manager')->getStorage('node');
// Grant the admin user required permissions. // Grant the admin user required permissions.
user_role_grant_permissions($this->admin_user->roles[0]->target_id, array('administer node fields')); user_role_grant_permissions($this->adminUser->roles[0]->target_id, array('administer node fields'));
$type_name = 'article'; $type_name = 'article';
$field_name = strtolower($this->randomMachineName()); $field_name = strtolower($this->randomMachineName());
...@@ -262,7 +262,7 @@ function testPrivateFileComment() { ...@@ -262,7 +262,7 @@ function testPrivateFileComment() {
$user = $this->drupalCreateUser(array('access comments')); $user = $this->drupalCreateUser(array('access comments'));
// Grant the admin user required comment permissions. // Grant the admin user required comment permissions.
$roles = $this->admin_user->getRoles(); $roles = $this->adminUser->getRoles();
user_role_grant_permissions($roles[1], array('administer comment fields', 'administer comments')); user_role_grant_permissions($roles[1], array('administer comment fields', 'administer comments'));
// Revoke access comments permission from anon user, grant post to // Revoke access comments permission from anon user, grant post to
...@@ -318,7 +318,7 @@ function testPrivateFileComment() { ...@@ -318,7 +318,7 @@ function testPrivateFileComment() {
$this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.'); $this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
// Unpublishes node. // Unpublishes node.
$this->drupalLogin($this->admin_user); $this->drupalLogin($this->adminUser);
$this->drupalPostForm('node/' . $node->id() . '/edit', array(), t('Save and unpublish')); $this->drupalPostForm('node/' . $node->id() . '/edit', array(), t('Save and unpublish'));
// Ensures normal user can no longer download the file. // Ensures normal user can no longer download the file.
......
...@@ -23,11 +23,18 @@ class FileListingTest extends FileFieldTestBase { ...@@ -23,11 +23,18 @@ class FileListingTest extends FileFieldTestBase {
*/ */
public static $modules = array('views', 'file', 'image'); public static $modules = array('views', 'file', 'image');
/**
* An authenticated user.
*
* @var \Drupal\user\UserInterface
*/
protected $baseUser;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->admin_user = $this->drupalCreateUser(array('access files overview', 'bypass node access')); $this->adminUser = $this->drupalCreateUser(array('access files overview', 'bypass node access'));
$this->base_user = $this->drupalCreateUser(); $this->baseUser = $this->drupalCreateUser();
$this->createFileField('file', 'node', 'article', array(), array('file_extensions' => 'txt png')); $this->createFileField('file', 'node', 'article', array(), array('file_extensions' => 'txt png'));
} }
...@@ -58,12 +65,12 @@ protected function sumUsages($usage) { ...@@ -58,12 +65,12 @@ protected function sumUsages($usage) {
function testFileListingPages() { function testFileListingPages() {
$file_usage = $this->container->get('file.usage'); $file_usage = $this->container->get('file.usage');
// Users without sufficient permissions should not see file listing. // Users without sufficient permissions should not see file listing.
$this->drupalLogin($this->base_user); $this->drupalLogin($this->baseUser);
$this->drupalGet('admin/content/files'); $this->drupalGet('admin/content/files');
$this->assertResponse(403); $this->assertResponse(403);
// Login with user with right permissions and test listing. // Login with user with right permissions and test listing.
$this->drupalLogin($this->admin_user); $this->drupalLogin($this->adminUser);
for ($i = 0; $i < 5; $i++) { for ($i = 0; $i < 5; $i++) {
$nodes[] = $this->drupalCreateNode(array('type' => 'article')); $nodes[] = $this->drupalCreateNode(array('type' => 'article'));
......
...@@ -62,7 +62,7 @@ function testPrivateFile() { ...@@ -62,7 +62,7 @@ function testPrivateFile() {
$no_access_field_name = 'field_no_view_access'; $no_access_field_name = 'field_no_view_access';
$this->createFileField($no_access_field_name, 'node', $type_name, array('uri_scheme' => 'private')); $this->createFileField($no_access_field_name, 'node', $type_name, array('uri_scheme' => 'private'));
// Test with the field that should deny access through field access. // Test with the field that should deny access through field access.
$this->drupalLogin($this->admin_user); $this->drupalLogin($this->adminUser);
$nid = $this->uploadNodeFile($test_file, $no_access_field_name, $type_name, TRUE, array('private' => TRUE)); $nid = $this->uploadNodeFile($test_file, $no_access_field_name, $type_name, TRUE, array('private' => TRUE));
\Drupal::entityManager()->getStorage('node')->resetCache(array($nid)); \Drupal::entityManager()->getStorage('node')->resetCache(array($nid));
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
......
...@@ -54,7 +54,7 @@ function testFileTokenReplacement() { ...@@ -54,7 +54,7 @@ function testFileTokenReplacement() {
$tests['[file:created:short]'] = format_date($file->getCreatedTime(), 'short', '', NULL, $language_interface->getId()); $tests['[file:created:short]'] = format_date($file->getCreatedTime(), 'short', '', NULL, $language_interface->getId());
$tests['[file:changed]'] = format_date($file->getChangedTime(), 'medium', '', NULL, $language_interface->getId()); $tests['[file:changed]'] = format_date($file->getChangedTime(), 'medium', '', NULL, $language_interface->getId());
$tests['[file:changed:short]'] = format_date($file->getChangedTime(), 'short', '', NULL, $language_interface->getId()); $tests['[file:changed:short]'] = format_date($file->getChangedTime(), 'short', '', NULL, $language_interface->getId());
$tests['[file:owner]'] = String::checkPlain(user_format_name($this->admin_user)); $tests['[file:owner]'] = String::checkPlain(user_format_name($this->adminUser));
$tests['[file:owner:uid]'] = $file->getOwnerId(); $tests['[file:owner:uid]'] = $file->getOwnerId();
// Test to make sure that we generated something for each token. // Test to make sure that we generated something for each token.
......
...@@ -22,6 +22,8 @@ class SaveUploadTest extends FileManagedTestBase { ...@@ -22,6 +22,8 @@ class SaveUploadTest extends FileManagedTestBase {
/** /**
* An image file path for uploading. * An image file path for uploading.
*
* @var \Drupal\file\FileInterface
*/ */
protected $image; protected $image;
...@@ -35,6 +37,13 @@ class SaveUploadTest extends FileManagedTestBase { ...@@ -35,6 +37,13 @@ class SaveUploadTest extends FileManagedTestBase {
*/ */
protected $maxFidBefore; protected $maxFidBefore;
/**
* Extension of the image filename.
*
* @var string
*/
protected $imageExtension;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$account = $this->drupalCreateUser(array('access site reports')); $account = $this->drupalCreateUser(array('access site reports'));
...@@ -43,7 +52,7 @@ protected function setUp() { ...@@ -43,7 +52,7 @@ protected function setUp() {
$image_files = $this->drupalGetTestFiles('image'); $image_files = $this->drupalGetTestFiles('image');
$this->image = entity_create('file', (array) current($image_files)); $this->image = entity_create('file', (array) current($image_files));
list(, $this->image_extension) = explode('.', $this->image->getFilename()); list(, $this->imageExtension) = explode('.', $this->image->getFilename());
$this->assertTrue(is_file($this->image->getFileUri()), "The image file we're going to upload exists."); $this->assertTrue(is_file($this->image->getFileUri()), "The image file we're going to upload exists.");
$this->phpfile = current($this->drupalGetTestFiles('php')); $this->phpfile = current($this->drupalGetTestFiles('php'));
...@@ -142,7 +151,7 @@ function testHandleExtension() { ...@@ -142,7 +151,7 @@ function testHandleExtension() {
// Reset the hook counters. // Reset the hook counters.
file_test_reset(); file_test_reset();
$extensions = 'foo ' . $this->image_extension; $extensions = 'foo ' . $this->imageExtension;
// Now tell file_save_upload() to allow the extension of our test image. // Now tell file_save_upload() to allow the extension of our test image.
$edit = array( $edit = array(
'file_test_replace' => FILE_EXISTS_REPLACE, 'file_test_replace' => FILE_EXISTS_REPLACE,
...@@ -225,12 +234,12 @@ function testHandleDangerousFile() { ...@@ -225,12 +234,12 @@ function testHandleDangerousFile() {
function testHandleFileMunge() { function testHandleFileMunge() {
// Ensure insecure uploads are disabled for this test. // Ensure insecure uploads are disabled for this test.
$this->config('system.file')->set('allow_insecure_uploads', 0)->save(); $this->config('system.file')->set('allow_insecure_uploads', 0)->save();
$this->image = file_move($this->image, $this->image->getFileUri() . '.foo.' . $this->image_extension); $this->image = file_move($this->image, $this->image->getFileUri() . '.foo.' . $this->imageExtension);
// Reset the hook counters to get rid of the 'move' we just called. // Reset the hook counters to get rid of the 'move' we just called.
file_test_reset(); file_test_reset();
$extensions = $this->image_extension; $extensions = $this->imageExtension;
$edit = array( $edit = array(
'files[file_test_upload]' => drupal_realpath($this->image->getFileUri()), 'files[file_test_upload]' => drupal_realpath($this->image->getFileUri()),
'extensions' => $extensions, 'extensions' => $extensions,
...@@ -238,7 +247,7 @@ function testHandleFileMunge() { ...@@ -238,7 +247,7 @@ function testHandleFileMunge() {
$munged_filename = $this->image->getFilename(); $munged_filename = $this->image->getFilename();
$munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.')); $munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.'));
$munged_filename .= '_.' . $this->image_extension; $munged_filename .= '_.' . $this->imageExtension;
$this->drupalPostForm('file-test/upload', $edit, t('Submit')); $this->drupalPostForm('file-test/upload', $edit, t('Submit'));
$this->assertResponse(200, 'Received a 200 response for posted test file.'); $this->assertResponse(200, 'Received a 200 response for posted test file.');
......
...@@ -15,14 +15,18 @@ ...@@ -15,14 +15,18 @@
class ValidatorTest extends FileManagedUnitTestBase { class ValidatorTest extends FileManagedUnitTestBase {
/** /**
* @var \Drupal\file\Entity\File * An image file.
*
* @var \Drupal\file\FileInterface
*/ */
protected $image; protected $image;
/** /**
* A file which is not an image.
*
* @var \Drupal\file\Entity\File * @var \Drupal\file\Entity\File
*/ */
protected $non_image; protected $nonImage;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
...@@ -31,9 +35,9 @@ protected function setUp() { ...@@ -31,9 +35,9 @@ protected function setUp() {
$this->image->setFileUri('core/misc/druplicon.png'); $this->image->setFileUri('core/misc/druplicon.png');
$this->image->setFilename(drupal_basename($this->image->getFileUri())); $this->image->setFilename(drupal_basename($this->image->getFileUri()));
$this->non_image = entity_create('file'); $this->nonImage = entity_create('file');
$this->non_image->setFileUri('core/assets/vendor/jquery/jquery.min.js'); $this->nonImage->setFileUri('core/assets/vendor/jquery/jquery.min.js');
$this->non_image->setFilename(drupal_basename($this->non_image->getFileUri())); $this->nonImage->setFilename(drupal_basename($this->nonImage->getFileUri()));
} }
/** /**
...@@ -57,8 +61,8 @@ function testFileValidateIsImage() { ...@@ -57,8 +61,8 @@ function testFileValidateIsImage() {
$errors = file_validate_is_image($this->image); $errors = file_validate_is_image($this->image);
$this->assertEqual(count($errors), 0, 'No error reported for our image file.', 'File'); $this->assertEqual(count($errors), 0, 'No error reported for our image file.', 'File');
$this->assertTrue(file_exists($this->non_image->getFileUri()), 'The non-image being tested exists.', 'File'); $this->assertTrue(file_exists($this->nonImage->getFileUri()), 'The non-image being tested exists.', 'File');
$errors = file_validate_is_image($this->non_image); $errors = file_validate_is_image($this->nonImage);
$this->assertEqual(count($errors), 1, 'An error reported for our non-image file.', 'File'); $this->assertEqual(count($errors), 1, 'An error reported for our non-image file.', 'File');
} }
...@@ -68,9 +72,9 @@ function testFileValidateIsImage() { ...@@ -68,9 +72,9 @@ function testFileValidateIsImage() {
*/ */
function testFileValidateImageResolution() { function testFileValidateImageResolution() {
// Non-images. // Non-images.
$errors = file_validate_image_resolution($this->non_image); $errors = file_validate_image_resolution($this->nonImage);
$this->assertEqual(count($errors), 0, 'Should not get any errors for a non-image file.', 'File'); $this->assertEqual(count($errors), 0, 'Should not get any errors for a non-image file.', 'File');
$errors = file_validate_image_resolution($this->non_image, '50x50', '100x100'); $errors = file_validate_image_resolution($this->nonImage, '50x50', '100x100');
$this->assertEqual(count($errors), 0, 'Do not check the resolution on non files.', 'File'); $this->assertEqual(count($errors), 0, 'Do not check the resolution on non files.', 'File');
// Minimum size. // Minimum size.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment