You are here

upload_tests.test in SimpleTest 6

Same filename and directory in other branches
  1. 5 tests/upload_tests.test

File

tests/upload_tests.test
View source
<?php

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);
    }
  }

}

Classes

Namesort descending Description
UploadPictureTests