You are here

class UploadPictureTests in SimpleTest 6

Same name and namespace in other branches
  1. 5 tests/upload_tests.test \UploadPictureTests

Hierarchy

Expanded class hierarchy of UploadPictureTests

File

tests/upload_tests.test, line 2

View source
class UploadPictureTests extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => 'Upload user picture',
      'desc' => 'Assure that dimension check, extension check and image scaling work as designed.',
      'group' => 'Upload Tests',
    );
  }

  /*
   * Test if directories specified in settings exist in filesystem
   */
  function testDirectories() {

    // test if filepath is proper
    $file_dir = file_directory_path();
    $picture_dir = variable_get('user_picture_path', 'pictures');
    $file_check = file_check_directory($file_dir, FILE_CREATE_DIRECTORY, 'file_directory_path');
    $picture_path = $file_dir . '/' . $picture_dir;
    $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path');

    // check directories

    //$this->assertTrue($file_check,"The directory $file_dir doesn't exist or cannot be created.");

    //$this->assertTrue($pic_check,"The directory $picture_path doesn't exist or cannot be created.");
    $this->_directory_test = is_writable($picture_path);
    $this
      ->assertTrue($this->_directory_test, "The directory {$picture_path} doesn't exist or is not writable. Further tests won't be made.");
  }
  function testNoPicture() {
    $old_pic_set = variable_get('user_pictures', 0);
    variable_set('user_pictures', 1);

    /* Prepare a user to do the stuff */
    $user = $this
      ->drupalCreateUserRolePerm(array(
      'access content',
    ));
    $this
      ->drupalLoginUser($user);

    // not a image
    $img_path = realpath(drupal_get_path('module', 'simpletest') . "/tests/upload_tests.test");
    $edit = array(
      'files[picture_upload]' => $img_path,
    );
    $this
      ->drupalPost('user/' . $user->uid . '/edit', $edit, 'Save');
    $this
      ->assertWantedRaw(t('The selected file %file could not be uploaded. Only JPEG, PNG and GIF images are allowed.', array(
      '%file' => 'upload_tests.test',
    )), 'The uploaded file was not an image.');
    variable_set('user_pictures', $old_pic_set);

    // do we have to check users roles?
    // delete test user and roles
  }

  /*
   * Do one test if ImageGDToolkit is installed
   */

  /*
   * Do the test:
   *  GD Toolkit is installed
   *  Picture has invalid dimension
   *
   * results: The image should be uploaded because ImageGDToolkit resizes the picture
   */
  function testWithGDinvalidDimension() {
    if ($this->_directory_Test) {
      if (image_get_toolkit()) {

        // PREPARE:
        $old_pic_set = variable_get('user_pictures', 0);
        variable_set('user_pictures', 1);

        /* Prepare a user to do the stuff */
        $user = $this
          ->drupalCreateUserRolePerm(array(
          'access content',
        ));
        $this
          ->drupalLoginUser($user);

        // changing actual setting;
        $old_dim = variable_get('user_picture_dimensions', '85x85');
        $old_size = variable_get('user_picture_file_size', '30');
        $img_path = realpath("modules/tests/pictureTesting.jpg");
        $info = image_get_info($img_path);

        // set new variables;
        $test_size = floor(filesize($img_path) / 1000) + 1;
        $test_dim = $info['width'] - 10 . 'x' . ($info['height'] - 10);
        variable_set('user_picture_dimensions', $test_dim);
        variable_set('user_picture_file_size', $test_size);

        // TEST:
        $edit = array(
          'picture' => $img_path,
        );
        $this
          ->drupalPost('user/' . $user->uid . '/edit', $edit, 'Save');
        $file_dir = variable_get('file_directory_path', 'files');
        $picture_dir = variable_get('user_picture_path', 'pictures');
        $pic_path = $file_dir . '/' . $picture_dir . '/picture-' . $user->uid . '.jpg';

        // get full url to the user's image
        $picture = file_create_url($pic_path);

        // check if image is displayed in user's profile page
        $content = $this->_browser
          ->getContent();
        $this
          ->assertTrue(strpos($content, $picture), "Image is displayed in user's profile page");

        // check if file is located in proper directory
        $this
          ->assertTrue(is_file($pic_path), "File is located in proper directory");

        // RESTORING:
        variable_set('user_picture_file_size', $old_size);
        variable_set('user_picture_dimensions', $old_dim);
        variable_set('user_pictures', $old_pic_set);
      }
    }
  }

  /*
   * Do the test:
   *  GD Toolkit is installed
   *  Picture has invalid size
   *
   * results: The image should be uploaded because ImageGDToolkit resizes the picture
   */
  function testWithGDinvalidSize() {
    if ($this->_directory_Test) {
      if (image_get_toolkit()) {

        // PREPARE:
        $old_pic_set = variable_get('user_pictures', 0);
        variable_set('user_pictures', 1);

        /* Prepare a user to do the stuff */
        $user = $this
          ->drupalCreateUserRolePerm(array(
          'access content',
        ));
        $this
          ->drupalLoginUser($user);

        // changing actual setting;
        $old_dim = variable_get('user_picture_dimensions', '85x85');
        $old_size = variable_get('user_picture_file_size', '30');
        $img_path = realpath("modules/tests/pictureTesting.jpg");
        $info = image_get_info($img_path);

        // set new variables;
        $test_dim = $info['width'] + 10 . 'x' . ($info['height'] + 10);
        $test_size = floor(filesize($img_path) / 1000) - 1;
        variable_set('user_picture_dimensions', $test_dim);
        variable_set('user_picture_file_size', $test_size);

        // TEST:
        $edit = array(
          'picture' => $img_path,
        );
        $this
          ->drupalPost('user/' . $user->uid . '/edit', $edit, 'Save');
        $file_dir = variable_get('file_directory_path', 'files');
        $picture_dir = variable_get('user_picture_path', 'pictures');
        $pic_path = $file_dir . '/' . $picture_dir . '/picture-' . $user->uid . '.jpg';

        // get full url to the user's image
        $picture = file_create_url($pic_path);

        // check if image is displayed in user's profile page
        $content = $this->_browser
          ->getContent();
        $this
          ->assertTrue(strpos($content, $picture), "Image is displayed in user's profile page");

        // check if file is located in proper directory
        $this
          ->assertTrue(is_file($pic_path), "File is located in proper directory");

        // RESTORING:
        variable_set('user_picture_file_size', $old_size);
        variable_set('user_picture_dimensions', $old_dim);
        variable_set('user_pictures', $old_pic_set);
      }
    }
  }

  /*
   * Do the test:
   *  GD Toolkit is not installed
   *  Picture has invalid size
   *
   * results: The image shouldn't be uploaded
   */
  function testWithoutGDinvalidDimension() {
    if ($this->_directory_test) {
      if (!image_get_toolkit()) {

        // PREPARE:
        $old_pic_set = variable_get('user_pictures', 0);
        variable_set('user_pictures', 1);

        /* Prepare a user to do the stuff */
        $user = $this
          ->drupalCreateUserRolePerm(array(
          'access content',
        ));
        $this
          ->drupalLoginUser($user);

        // changing actual setting;
        $old_dim = variable_get('user_picture_dimensions', '85x85');
        $old_size = variable_get('user_picture_file_size', '30');
        $img_path = realpath("modules/tests/pictureTesting.jpg");
        $info = image_get_info($img_path);

        // set new variables;
        $test_size = floor(filesize($img_path) / 1000) + 1;
        $test_dim = $info['width'] - 10 . 'x' . ($info['height'] - 10);
        variable_set('user_picture_dimensions', $test_dim);
        variable_set('user_picture_file_size', $test_size);

        // TEST:
        $edit = array(
          'picture' => $img_path,
        );
        $this
          ->drupalPost('user/' . $user->uid . '/edit', $edit, 'Save');
        $text = t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array(
          '%dimensions' => variable_get('user_picture_dimensions', '85x85'),
        ));
        $this
          ->assertWantedText($text, 'Checking response on invalid image (dimensions).');

        // check if file is not uploaded
        $file_dir = variable_get('file_directory_path', 'files');
        $picture_dir = variable_get('user_picture_path', 'pictures');
        $pic_path = $file_dir . '/' . $picture_dir . '/picture-' . $user->uid . '.jpg';
        $this
          ->assertFalse(is_file($pic_path), "File is not uploaded");

        // restore variables;
        variable_set('user_picture_file_size', $old_size);
        variable_set('user_picture_dimensions', $old_dim);
        variable_set('user_pictures', $old_pic_set);
      }
    }
  }

  /*
   * Do the test:
   *  GD Toolkit is not installed
   *  Picture has invalid size
   *
   * results: The image shouldn't be uploaded
   */
  function testWithoutGDinvalidSize() {
    if ($this->_directory_test) {
      if (!image_get_toolkit()) {

        // PREPARE:
        $old_pic_set = variable_get('user_pictures', 0);
        variable_set('user_pictures', 1);

        /* Prepare a user to do the stuff */
        $user = $this
          ->drupalCreateUserRolePerm(array(
          'access content',
        ));
        $this
          ->drupalLoginUser($user);

        // changing actual setting;
        $old_dim = variable_get('user_picture_dimensions', '85x85');
        $old_size = variable_get('user_picture_file_size', '30');
        $img_path = realpath("modules/tests/pictureTesting.jpg");
        $info = image_get_info($img_path);

        // invalid size
        // restore one and set another
        $test_dim = $info['width'] + 10 . 'x' . ($info['height'] + 10);
        $test_size = floor(filesize($img_path) / 1000) - 1;
        variable_set('user_picture_dimensions', $test_dim);
        variable_set('user_picture_file_size', $test_size);
        $edit = array(
          'picture' => $img_path,
        );
        $this
          ->drupalPost('user/' . $user->uid . '/edit', $edit, 'Save');
        $text = t('The uploaded image is too large; the maximum file size is %size kB.', array(
          '%size' => variable_get('user_picture_file_size', '30'),
        ));
        $this
          ->assertWantedText($text, 'Checking response on invalid image size.');

        // check if file is not uploaded
        $file_dir = variable_get('file_directory_path', 'files');
        $picture_dir = variable_get('user_picture_path', 'pictures');
        $pic_path = $file_dir . '/' . $picture_dir . '/picture-' . $user->uid . '.jpg';
        $this
          ->assertFalse(is_file($pic_path), "File is not uploaded");

        // restore variables;
        variable_set('user_picture_file_size', $old_size);
        variable_set('user_picture_dimensions', $old_dim);
        variable_set('user_pictures', $old_pic_set);
      }
    }
  }

  /*
   * Do the test:
   *  Picture is valid (proper size and dimension)
   *
   * results: The image should be uploaded
   */
  function testPictureIsValid() {
    if ($this->_directory_test) {

      // PREPARE:
      $old_pic_set = variable_get('user_pictures', 0);
      variable_set('user_pictures', 1);

      /* Prepare a user to do the stuff */
      $user = $this
        ->drupalCreateUserRolePerm(array(
        'access content',
      ));
      $this
        ->drupalLoginUser($user);

      // changing actual setting;
      $old_dim = variable_get('user_picture_dimensions', '85x85');
      $old_size = variable_get('user_picture_file_size', '30');
      $img_path = realpath(drupal_get_path('module', 'simpletest') . "/tests/pictureTesting.jpg");
      $info = image_get_info($img_path);

      // valid size & dimensions
      // restore one and set another
      $test_dim = $info['width'] + 10 . 'x' . ($info['height'] + 10);
      $test_size = floor(filesize($img_path) / 1000) + 1;
      variable_set('user_picture_dimensions', $test_dim);
      variable_set('user_picture_file_size', $test_size);

      // TEST:
      $edit = array(
        'files[picture_upload]' => $img_path,
      );
      $this
        ->drupalPost('user/' . $user->uid . '/edit', $edit, 'Save');
      $picture_dir = variable_get('user_picture_path', 'pictures');
      $pic_path = file_directory_path() . '/' . $picture_dir . '/picture-' . $user->uid . '.jpg';

      // get full url to the user's image
      $picture = file_create_url($pic_path);

      // check if image is displayed in user's profile page
      $content = $this
        ->drupalGetContent();
      $this
        ->assertTrue(strpos($content, $picture), "Image is displayed in user's profile page");

      // check if file is located in proper directory
      $this
        ->assertTrue(is_file($pic_path), "File is located in proper directory");

      // RESTORING:
      variable_set('user_picture_file_size', $old_size);
      variable_set('user_picture_dimensions', $old_dim);
      variable_set('user_pictures', $old_pic_set);

      // DELETING UPLOADED PIC
      file_delete($pic_path);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$_cleanupContentTypes property
DrupalTestCase::$_cleanupNodes property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::$_modules property
DrupalTestCase::$_originalModules property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::checkOriginalModules function Retrieves and saves current modules list into $_originalModules and $_modules.
DrupalTestCase::clickLink function Follows a link by name.
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateContentType function Creates a custom content type based on default settings.
DrupalTestCase::drupalCreateNode function Creates a node based on default settings.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Broker for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPost function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser By $reporting you specify if this request does assertions or not Warning: empty ("") returns will cause fails with $reporting
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a drupal variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter
DrupalTestCase::tearDown function tearDown implementation, setting back switched modules etc 8
UploadPictureTests::get_info function
UploadPictureTests::testDirectories function
UploadPictureTests::testNoPicture function
UploadPictureTests::testPictureIsValid function
UploadPictureTests::testWithGDinvalidDimension function
UploadPictureTests::testWithGDinvalidSize function
UploadPictureTests::testWithoutGDinvalidDimension function
UploadPictureTests::testWithoutGDinvalidSize function