Added DB Access + Error page
This commit is contained in:
25
demos/blog/protected/tests/WebTestCase.php
Normal file
25
demos/blog/protected/tests/WebTestCase.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Change the following URL based on your server configuration
|
||||
* Make sure the URL ends with a slash so that we can use relative URLs in test cases
|
||||
*/
|
||||
define('TEST_BASE_URL','http://localhost/yii/demos/blog/index-test.php/');
|
||||
|
||||
/**
|
||||
* The base class for functional test cases.
|
||||
* In this class, we set the base URL for the test application.
|
||||
* We also provide some common methods to be used by concrete test classes.
|
||||
*/
|
||||
class WebTestCase extends CWebTestCase
|
||||
{
|
||||
/**
|
||||
* Sets up before each test method runs.
|
||||
* This mainly sets the base URL for the test application.
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->setBrowserUrl(TEST_BASE_URL);
|
||||
}
|
||||
}
|
||||
10
demos/blog/protected/tests/bootstrap.php
Normal file
10
demos/blog/protected/tests/bootstrap.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
// change the following paths if necessary
|
||||
$yiit=dirname(__FILE__).'/../../../../framework/yiit.php';
|
||||
$config=dirname(__FILE__).'/../config/test.php';
|
||||
|
||||
require_once($yiit);
|
||||
require_once(dirname(__FILE__).'/WebTestCase.php');
|
||||
|
||||
Yii::createWebApplication($config);
|
||||
24
demos/blog/protected/tests/fixtures/tbl_comment.php
vendored
Normal file
24
demos/blog/protected/tests/fixtures/tbl_comment.php
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
/*
|
||||
'sample1'=>array(
|
||||
'content' => '',
|
||||
'status' => '',
|
||||
'create_time' => '',
|
||||
'author' => '',
|
||||
'email' => '',
|
||||
'url' => '',
|
||||
'post_id' => '',
|
||||
),
|
||||
'sample2'=>array(
|
||||
'content' => '',
|
||||
'status' => '',
|
||||
'create_time' => '',
|
||||
'author' => '',
|
||||
'email' => '',
|
||||
'url' => '',
|
||||
'post_id' => '',
|
||||
),
|
||||
*/
|
||||
);
|
||||
18
demos/blog/protected/tests/fixtures/tbl_lookup.php
vendored
Normal file
18
demos/blog/protected/tests/fixtures/tbl_lookup.php
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
/*
|
||||
'sample1'=>array(
|
||||
'name' => '',
|
||||
'code' => '',
|
||||
'type' => '',
|
||||
'position' => '',
|
||||
),
|
||||
'sample2'=>array(
|
||||
'name' => '',
|
||||
'code' => '',
|
||||
'type' => '',
|
||||
'position' => '',
|
||||
),
|
||||
*/
|
||||
);
|
||||
13
demos/blog/protected/tests/fixtures/tbl_post.php
vendored
Normal file
13
demos/blog/protected/tests/fixtures/tbl_post.php
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
'sample1'=>array(
|
||||
'title'=>'test post 1',
|
||||
'content'=>"This blog is powered by [Yii framework](http://www.yiiframework.com).",
|
||||
'status'=>2,
|
||||
'create_time'=>1230952187,
|
||||
'update_time'=>1230952187,
|
||||
'author_id'=>1,
|
||||
'tags'=>'yii, blog',
|
||||
),
|
||||
);
|
||||
12
demos/blog/protected/tests/fixtures/tbl_tag.php
vendored
Normal file
12
demos/blog/protected/tests/fixtures/tbl_tag.php
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
array(
|
||||
'name'=>'yii',
|
||||
'frequency'=>1,
|
||||
),
|
||||
array(
|
||||
'name'=>'blog',
|
||||
'frequency'=>1,
|
||||
),
|
||||
);
|
||||
9
demos/blog/protected/tests/fixtures/tbl_user.php
vendored
Normal file
9
demos/blog/protected/tests/fixtures/tbl_user.php
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
array(
|
||||
'username'=>'demo',
|
||||
'password'=>'$2a$10$JTJf6/XqC94rrOtzuF397OHa4mbmZrVTBOQCmYD9U.obZRUut4BoC',
|
||||
'email'=>'webmaster@example.com',
|
||||
),
|
||||
);
|
||||
36
demos/blog/protected/tests/functional/CommentTest.php
Normal file
36
demos/blog/protected/tests/functional/CommentTest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
class CommentTest extends WebTestCase
|
||||
{
|
||||
/**
|
||||
* We use both 'Post' and 'Comment' fixtures.
|
||||
* @see CWebTestCase::fixtures
|
||||
*/
|
||||
public $fixtures=array(
|
||||
'posts'=>'Post',
|
||||
'comments'=>'Comment',
|
||||
);
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$this->open('post/1/xyz');
|
||||
|
||||
// verify the sample post title exists
|
||||
$this->assertTextPresent($this->posts['sample1']['title']);
|
||||
$this->assertElementPresent("name=Comment[author]");
|
||||
|
||||
// verify validation errors
|
||||
$this->clickAndWait("//input[@value='Submit']");
|
||||
$this->assertTextPresent('Name cannot be blank.');
|
||||
$this->assertTextPresent('Email cannot be blank.');
|
||||
$this->assertTextPresent('Comment cannot be blank.');
|
||||
|
||||
// verify commenting is successful
|
||||
$comment="comment 1";
|
||||
$this->type('name=Comment[author]','me');
|
||||
$this->type('name=Comment[email]','me@example.com');
|
||||
$this->type('name=Comment[content]',$comment);
|
||||
$this->clickAndWait("//input[@value='Submit']");
|
||||
$this->assertTextPresent('Thank you for your comment');
|
||||
}
|
||||
}
|
||||
30
demos/blog/protected/tests/functional/PostTest.php
Normal file
30
demos/blog/protected/tests/functional/PostTest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
class PostTest extends WebTestCase
|
||||
{
|
||||
/**
|
||||
* We use the 'Post' only for this test.
|
||||
* @see CWebTestCase::fixtures
|
||||
*/
|
||||
public $fixtures=array(
|
||||
'posts'=>'Post',
|
||||
);
|
||||
|
||||
public function testIndex()
|
||||
{
|
||||
$this->open('');
|
||||
// verify header title exists
|
||||
$this->assertTextPresent('Yii Blog Demo');
|
||||
// verify the sample post title exists
|
||||
$this->assertTextPresent($this->posts['sample1']['title']);
|
||||
}
|
||||
|
||||
public function testView()
|
||||
{
|
||||
$this->open('post/1/xyz');
|
||||
// verify the sample post title exists
|
||||
$this->assertTextPresent($this->posts['sample1']['title']);
|
||||
// verify comment form exists
|
||||
$this->assertTextPresent('Leave a Comment');
|
||||
}
|
||||
}
|
||||
41
demos/blog/protected/tests/functional/SiteTest.php
Normal file
41
demos/blog/protected/tests/functional/SiteTest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
class SiteTest extends WebTestCase
|
||||
{
|
||||
public function testContact()
|
||||
{
|
||||
$this->open('site/contact');
|
||||
$this->assertTextPresent('Contact Us');
|
||||
$this->assertElementPresent('name=ContactForm[name]');
|
||||
|
||||
$this->type('name=ContactForm[name]','tester');
|
||||
$this->type('name=ContactForm[email]','tester@example.com');
|
||||
$this->type('name=ContactForm[subject]','test subject');
|
||||
$this->clickAndWait("//input[@value='Submit']");
|
||||
$this->assertTextPresent('Body cannot be blank.');
|
||||
}
|
||||
|
||||
public function testLoginLogout()
|
||||
{
|
||||
$this->open('');
|
||||
// ensure the user is logged out
|
||||
if($this->isTextPresent('Logout'))
|
||||
$this->clickAndWait('link=Logout');
|
||||
|
||||
// test login process, including validation
|
||||
$this->clickAndWait('link=Login');
|
||||
$this->assertElementPresent('name=LoginForm[username]');
|
||||
$this->type('name=LoginForm[username]','demo');
|
||||
$this->clickAndWait("//input[@value='Login']");
|
||||
$this->assertTextPresent('Password cannot be blank.');
|
||||
$this->type('name=LoginForm[password]','demo');
|
||||
$this->clickAndWait("//input[@value='Login']");
|
||||
$this->assertTextNotPresent('Password cannot be blank.');
|
||||
$this->assertTextPresent('Logout');
|
||||
|
||||
// test logout process
|
||||
$this->assertTextNotPresent('Login');
|
||||
$this->clickAndWait('link=Logout');
|
||||
$this->assertTextPresent('Login');
|
||||
}
|
||||
}
|
||||
13
demos/blog/protected/tests/phpunit.xml
Normal file
13
demos/blog/protected/tests/phpunit.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<phpunit bootstrap="bootstrap.php"
|
||||
colors="false"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
stopOnFailure="false">
|
||||
|
||||
<selenium>
|
||||
<browser name="Internet Explorer" browser="*iexplore" />
|
||||
<browser name="Firefox" browser="*firefox" />
|
||||
</selenium>
|
||||
|
||||
</phpunit>
|
||||
42
demos/blog/protected/tests/readme.txt
Normal file
42
demos/blog/protected/tests/readme.txt
Normal file
@@ -0,0 +1,42 @@
|
||||
This directory contains unit and functional tests for the blog demo.
|
||||
|
||||
- fixtures: contains fixture data for relevant database tables.
|
||||
Each file is used to set up the fixture data for a particular table.
|
||||
The file name is the same as the table name.
|
||||
|
||||
- functional: contains functional test cases.
|
||||
|
||||
- unit: contains unit test cases.
|
||||
|
||||
- report: contains any coverage reports.
|
||||
|
||||
|
||||
In order to run these tests, the following requirements must be met:
|
||||
|
||||
- PHPUnit 3.3 or higher
|
||||
- Selenium RC 1.0 or higher
|
||||
|
||||
|
||||
Depending on your installation of Yii release, you may need to modify
|
||||
the file "WebTestCase.php" so that the "TEST_BASE_URL" constant contains
|
||||
correct value. You may also modify "phpunit.xml" to specify the browsers
|
||||
you intend to run the functional tests on.
|
||||
|
||||
To run these tests, please refer to PHPUnit documentation. The followings
|
||||
are some examples:
|
||||
|
||||
- Executes all tests under the "unit" directory with verbose information:
|
||||
|
||||
phpunit --verbose unit
|
||||
|
||||
- Executes all tests under "functional" (Selenium RC is running):
|
||||
|
||||
phpunit functional
|
||||
|
||||
- Executes a particular test:
|
||||
|
||||
phpunit functional/PostTest.php
|
||||
|
||||
|
||||
*DISCLAIMER* The test cases included here are by no means complete. They mainly serve
|
||||
for demonstrative purpose.
|
||||
57
demos/blog/protected/tests/unit/CommentTest.php
Normal file
57
demos/blog/protected/tests/unit/CommentTest.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
class CommentTest extends CDbTestCase
|
||||
{
|
||||
/**
|
||||
* We use both 'Post' and 'Comment' fixtures.
|
||||
* @see CWebTestCase::fixtures
|
||||
*/
|
||||
public $fixtures=array(
|
||||
'posts'=>'Post',
|
||||
'comments'=>'Comment',
|
||||
);
|
||||
|
||||
public function testFindRecentComments()
|
||||
{
|
||||
$this->assertEquals(array(), Comment::model()->findRecentComments());
|
||||
|
||||
$comment=new Comment;
|
||||
$comment->setAttributes(array(
|
||||
'content'=>'comment 1',
|
||||
'status'=>Comment::STATUS_APPROVED,
|
||||
'create_time'=>time(),
|
||||
'author'=>'me',
|
||||
'email'=>'me@example.com',
|
||||
'post_id'=>$this->posts['sample1']['id'],
|
||||
),false);
|
||||
$this->assertTrue($comment->save(false));
|
||||
$this->assertEquals(1,$comment->id);
|
||||
|
||||
$comments=Comment::model()->findRecentComments();
|
||||
$this->assertEquals(1,count($comments));
|
||||
$this->assertEquals($comment->attributes, $comments[0]->attributes);
|
||||
}
|
||||
|
||||
public function testApprove()
|
||||
{
|
||||
$comment=new Comment;
|
||||
$comment->setAttributes(array(
|
||||
'content'=>'comment 1',
|
||||
'status'=>Comment::STATUS_PENDING,
|
||||
'create_time'=>time(),
|
||||
'author'=>'me',
|
||||
'email'=>'me@example.com',
|
||||
'post_id'=>$this->posts['sample1']['id'],
|
||||
),false);
|
||||
$this->assertTrue($comment->save(false));
|
||||
|
||||
$comment=Comment::model()->findByPk($comment->id);
|
||||
$this->assertTrue($comment instanceof Comment);
|
||||
$this->assertEquals(Comment::STATUS_PENDING,$comment->status);
|
||||
|
||||
$comment->approve();
|
||||
$this->assertEquals(Comment::STATUS_APPROVED,$comment->status);
|
||||
$comment=Comment::model()->findByPk($comment->id);
|
||||
$this->assertEquals(Comment::STATUS_APPROVED,$comment->status);
|
||||
}
|
||||
}
|
||||
13
demos/blog/protected/tests/unit/LookupTest.php
Normal file
13
demos/blog/protected/tests/unit/LookupTest.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
class LookupTest extends CDbTestCase
|
||||
{
|
||||
public $fixtures=array(
|
||||
'lookups'=>'Lookup',
|
||||
);
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
18
demos/blog/protected/tests/unit/PostTest.php
Normal file
18
demos/blog/protected/tests/unit/PostTest.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
class PostTest extends CDbTestCase
|
||||
{
|
||||
/**
|
||||
* We use both 'Post' and 'Comment' fixtures.
|
||||
* @see CWebTestCase::fixtures
|
||||
*/
|
||||
public $fixtures=array(
|
||||
'posts'=>'Post',
|
||||
'comments'=>'Comment',
|
||||
);
|
||||
|
||||
public function testSave()
|
||||
{
|
||||
// write code here to test post saving method
|
||||
}
|
||||
}
|
||||
13
demos/blog/protected/tests/unit/TagTest.php
Normal file
13
demos/blog/protected/tests/unit/TagTest.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
class TagTest extends CDbTestCase
|
||||
{
|
||||
public $fixtures=array(
|
||||
'tags'=>'Tag',
|
||||
);
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
24
demos/blog/protected/tests/unit/UserTest.php
Normal file
24
demos/blog/protected/tests/unit/UserTest.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
class UserTest extends CDbTestCase
|
||||
{
|
||||
public $fixtures=array(
|
||||
'users'=>'User',
|
||||
);
|
||||
|
||||
public function testValidatePassword()
|
||||
{
|
||||
$this->assertTrue($this->users(0)->validatePassword('demo'));
|
||||
$this->assertFalse($this->users(0)->validatePassword('wrong'));
|
||||
|
||||
}
|
||||
|
||||
public function testChangePassword()
|
||||
{
|
||||
$user=$this->users(0);
|
||||
$user->password=$user->hashPassword('newpwd');
|
||||
$this->assertFalse($user->validatePassword('demo'));
|
||||
$this->assertTrue($user->validatePassword('newpwd'));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user