You are here

class realistic_dummy_content_UnitTestCase in Realistic Dummy Content 7

The test case

Hierarchy

Expanded class hierarchy of realistic_dummy_content_UnitTestCase

File

api/tests/realistic_dummy_content_api.unit.test, line 37
This file contains the testing code for this module

View source
class realistic_dummy_content_UnitTestCase extends DrupalUnitTestCase {

  /**
   * Info for this test case.
   */
  public static function getInfo() {
    return array(
      'name' => t('Realistic dummy content unit test'),
      'description' => t('Test pure functions for Realistic dummy content.'),
      'group' => 'Realistic dummy content',
    );
  }
  public function setUp() {

    // specifically include files which contain functions to test.
    module_load_include('module', 'realistic_dummy_content_api');
    parent::setUp();
  }

  /*
   * Test case for realistic_dummy_content_api.
   */
  public function testModule() {

    // only pure functions should be tested here. The database is not available.
    $user = (object) array();
    $user->mail = 'whatever@example.com.invalid';
    $this
      ->assertTrue(realistic_dummy_content_api_realistic_dummy_content_api_dummy($user, 'user'), 'User with an email ending in .invalid is considered dummy content');
    $user->mail = 'whatever@example.com';
    $user->devel_generate = TRUE;
    $this
      ->assertTrue(realistic_dummy_content_api_realistic_dummy_content_api_dummy($user, 'user'), 'User with the devel_generate property set is considered dummy content');
    unset($user->devel_generate);
    $this
      ->assertFalse(realistic_dummy_content_api_realistic_dummy_content_api_dummy($user, 'user'), 'User with neither an address ending in .invalid nor the devel_generate property set is considered non-dummy');
    $node = (object) array();
    $node->devel_generate = array();
    $this
      ->assertTrue(realistic_dummy_content_api_realistic_dummy_content_api_dummy($node, 'node'), 'Node with the devel_generate property set to an empty array is considered dummy');
    unset($node->devel_generate);
    $this
      ->assertFALSE(realistic_dummy_content_api_realistic_dummy_content_api_dummy($node, 'node'), 'Node with the devel_generate not set is considered non-dummy');
    $this
      ->assertSequential(0, 3, 'a', 0);
    $this
      ->assertSequential(0, 3, 'a', 0);
    $this
      ->assertSequential(0, 3, 'b', 1);
    $this
      ->assertSequential(0, 3, 'b', 1);
    $this
      ->assertSequential(0, 3, 'c', 2);
    $this
      ->assertSequential(0, 3, 'c', 2);
    $this
      ->assertSequential(0, 3, 'd', 3);
    $this
      ->assertSequential(0, 2, 'd', 2);
    $this
      ->assertSequential(10, 13, 'd', 10);
    $this
      ->assertSequential(11, 12, 'd', 11);
  }

  /**
   * Helper function to assert that the sequential number generator works.
   *
   * This calls realistic_dummy_content_api_sequential(), making sure that the result
   * is as expected.
   *
   * @param $start
   *   Start number passed to realistic_dummy_content_api_sequential()
   * @param $end
   *   End number passed to realistic_dummy_content_api_sequential()
   * @param $hash
   *   Hash passed to realistic_dummy_content_api_sequential()
   * @param $expected
   *   Expected result which realistic_dummy_content_api_sequential() is expected
   *   to return.
   */
  function assertSequential($start, $end, $hash, $expected) {
    $result = realistic_dummy_content_api_sequential($start, $end, $hash);
    $this
      ->assertTrue($result == $expected, 'Sequential number is as expected for ' . $start . ', ' . $end . ' with hash ' . $hash . ': [expected] ' . $expected . ' = [result] ' . $result);
  }

  /**
   * Test that file names are properly parsed and combined.
   */
  function testFiles() {
    module_load_include('inc', 'realistic_dummy_content_api', 'includes/RealisticDummyContentEnvironment');
    $data = array(
      'one.txt' => new stdClass(),
      'reAdme.txt' => new stdClass(),
      'README.md' => new stdClass(),
      'readme.jpg' => new stdClass(),
      'two.txt' => new stdClass(),
      'two.notanattribute.txt' => new stdClass(),
      'two.txt.attribute.txt' => new stdClass(),
      'two.txt.attribute1.txt' => new stdClass(),
      'three.png' => new stdClass(),
      'three.png.alt.txt' => new stdClass(),
    );
    try {
      $parsed = RealisticDummyContentEnvironment::SortCandidateFiles($data);
      $parsed_images = RealisticDummyContentEnvironment::SortCandidateFiles($data, array(
        'png',
      ));
    } catch (Exception $e) {
      $this
        ->assertFalse(TRUE, 'Got exception ' . $e
        ->getMessage());
    }
    $this
      ->assertTrue(count($parsed) == 4, '4 parsed files are returned, which excludes the readme riles (4 == ' . count($parsed) . ')');
    $this
      ->assertTrue(is_object($parsed['one.txt']['file']));
    $this
      ->assertTrue(is_object($parsed['two.txt']['file']));
    $this
      ->assertTrue(is_object($parsed['two.txt']['attributes']['attribute']));
    $this
      ->assertTrue(is_object($parsed['two.txt']['attributes']['attribute1']));
    $this
      ->assertTrue(is_object($parsed['three.png']['file']));
    $this
      ->assertTrue(is_object($parsed['three.png']['attributes']['alt']));
    $this
      ->assertFalse(isset($parsed_images['two.txt']['attributes']['attribute1']));
    $this
      ->assertTrue(is_object($parsed_images['three.png']['file']));
    $this
      ->assertTrue(is_object($parsed_images['three.png']['attributes']['alt']));
    $this
      ->assertTrue(is_object($parsed['two.notanattribute.txt']['file']));
  }

  /**
   * Test that empty files and non-existing files are treated differently.
   */
  function testEmpty() {
    module_load_include('inc', 'realistic_dummy_content_api', 'includes/RealisticDummyContentAttribute');
    module_load_include('inc', 'realistic_dummy_content_api', 'includes/RealisticDummyContentField');
    module_load_include('inc', 'realistic_dummy_content_api', 'includes/RealisticDummyContentValueField');
    $field = new RealisticDummyContentValueField('ignore entity', 'ignore name');
    $null = new RealisticDummyContentUnitTestCaseDummyFile(NULL);
    $empty = new RealisticDummyContentUnitTestCaseDummyFile('');
    $this
      ->assertFalse(is_array($field
      ->ValueFromFile_($null)), 'No applicable field value is represented by NULL.');
    $this
      ->assertTrue(is_array($field
      ->ValueFromFile_($empty)), 'An empty string is considered a valid value.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::tearDown protected function 1
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct
realistic_dummy_content_UnitTestCase::assertSequential function Helper function to assert that the sequential number generator works.
realistic_dummy_content_UnitTestCase::getInfo public static function Info for this test case.
realistic_dummy_content_UnitTestCase::setUp public function Sets up unit test environment. Overrides DrupalUnitTestCase::setUp
realistic_dummy_content_UnitTestCase::testEmpty function Test that empty files and non-existing files are treated differently.
realistic_dummy_content_UnitTestCase::testFiles function Test that file names are properly parsed and combined.
realistic_dummy_content_UnitTestCase::testModule public function