Writing PHP unit-tests : How to add a fake user table entry

Flexibility of the Mediawiki PHP unit-tests wrapper extends to the fact that fake database entries can be made and tested upon without causing any harm to the actual one.
As I scribbled in the earlier post, the class-comments play a vital role, and dont forget to give them like this:

<?php
/**
 * Class to test table lookups
 *
 * @group Database
 */
class ModifyUserTableTest extends MediaWikiTestCase {
	function testModifyUserTable() {
        // Lets add a fake user named TestUser, and check that his email is confirmed and then get his User Id  
		$user = User::newFromName( 'TestUser' );
		$user->setEmail( 'bob@example.ext' );
		$user->addToDatabase();

		$user->confirmEmail();
		$user->saveSettings();

		$this->assertTrue( $user->isEmailConfirmed() );

		$uid = $user->getId();
        }
}

This would create a fake user TestUser in a fake table – mediawiki_unittest. Later this user can be used for any other testing purpose within the class ! Yay!

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s