You are on page 1of 46

IMPLEMENTATION AND TESTING

1|Page
Chapter 5

IMPLEMENTATION AND TESTING


5.1 Implementation approach
Iterative model is a particular implementation of a software development life cycle that
focuses on an initial, simplified implementation, which then progressively gains more
complexity and a broader feature set until the final system is complete. In short, iterative
development is a way of breaking down the software development of a large application into
smaller pieces.

5.2 Coding Details

login.php

<?php

class Login
{

private $error = "";

public function evaluate($data)


{

$email = addslashes($data['email']);
$password = addslashes($data['password']);

$query = "select * from users where email = '$email' limit 1 ";

$DB = new Database();


$result = $DB->read($query);

if ($result)
{

$row = $result[0];

if($this->hash_text($password) == $row['password'])
{

// Create Session data


$_SESSION['mybook_userid'] = $row['userid'];

}else

2|Page
{
$this->error .= "Wrong email or password <br>";
}
}else
{

$this->error .= "Wrong email or password <br>";


}

return $this->error;

private function hash_text($text){

$text = hash("sha1", $text);


return $text;
}

public function check_login($id)


{
if (is_numeric($id))
{

$query = "select * from users where userid = '$id' limit 1 ";

$DB = new Database();


$result = $DB->read($query);

if ($result)
{

$user_data = $result[0];
return $user_data;
}else
{
header("Location: login.php");
die;
}

}else
{
header("Location: login.php");
die;
}

3|Page
Signup.php
<?php

class Signup
{

private $error = "";

public function evaluate($data)


{

foreach ($data as $key => $value) {


# code...

if (empty($value))
{
$this->error = $this->error . $key . " is empty!<br>";
}

if ($key == "email")
{
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$value)) {

$this->error = $this->error . $key . " address invalid <br>";


}
}

if ($key == "first_name")
{
if (is_numeric($value)) {

$this->error = $this->error . $key . " cannot have numeric value.


<br>";
}

if (strstr($value, " ")) {

$this->error = $this->error . $key . " cannot have spaces. <br>";

if ($key == "last_name")
{
if (is_numeric($value)) {

4|Page
$this->error = $this->error . $key . " cannot have numeric value.
<br>";
}

if (strstr($value, " ")) {

$this->error = $this->error . $key . " cannot have spaces. <br>";

}
}

if ($this->error == "")
{

//no error
$this->create_user($data);
}else
{
return $this->error;
}
}

public function create_user($data)


{

$first_name = ucfirst($data['first_name']);
$last_name = ucfirst($data['last_name']);
$gender = $data['gender'];
$email = $data['email'];
$password = $data['password'];

//create these
$url_address = strtolower($first_name) . "." .strtolower($last_name);
$userid = $this->create_userid();

$query ="insert into users


(userid,first_name,last_name,gender,email,password,url_address)
values
('$userid','$first_name','$last_name','$gender','$email','$password','$url_address')";

$DB = new Database();


$DB->save($query);
}

5|Page
private function create_userid()
{

$length = rand(4,19);
$number = "";
for ($i=0; $i < $length ; $i++) {
# code...
$new_rand = rand(0,9);

$number = $number . $new_rand;


}

return $number;
}
}

6|Page
Connect.php

<?php

class Database
{

private $host = "localhost";


private $username = "root";
private $password = "";
private $db = "mybook_db";

function connect()
{

$connection = mysqli_connect($this->host,$this->username,$this->password,$this->db);
return $connection;
}

function read($query)
{
$conn = $this->connect();
$result = mysqli_query($conn,$query);

if (!$result)
{
return false;
}
else
{
$data = false;
while ($row = mysqli_fetch_assoc($result))
{

$data[] = $row;

return $data;
}
}

function save($query)
{
$conn = $this->connect();
$result = mysqli_query($conn,$query);

if(!$result)

7|Page
{
return false;
}else
{
return true;
}
}

8|Page
Post.php

<?php

class Post
{
private $error = "";

public function create_post($userid, $data , $files)


{

if (!empty($data['post']) || !empty($files['file']['name']) || isset($data['is_profile_image']) ||


isset($data['is_cover_image']))
{

$myimage = "";
$has_image = 0;
$is_cover_image = 0;
$is_profile_image = 0;

if (isset($data['is_profile_image']) || isset($data['is_cover_image']))
{

$myimage = $files;
$has_image = 1 ;

if (isset($data['is_cover_image']))
{
$is_cover_image = 1;
}

if (isset($data['is_profile_image']))
{
$is_profile_image = 1;
}

}else
{

if (!empty($files['file']['name']))
{

$folder = "uploads/" . $userid . "/";

//Create Folder
if (!file_exists($folder))
{

mkdir($folder,0777,true);

9|Page
file_put_contents($folder . "index.php", "");
}

$image_class = new Image();

$myimage = $folder . $image_class->generate_filename(15) .


"jpg";
move_uploaded_file($_FILES['file']['tmp_name'] , $myimage);

$image_class->resize_image($myimage,$myimage,1500,1500);

$has_image = 1;
}
}

$post = "";
if (isset($data['post']))
{
$post = addslashes($data['post']);
}

$postid = $this->create_postid();

$query = " insert into posts


(userid,postid,post,image,has_image,is_profile_image,is_cover_image) values
('$userid','$postid','$post','$myimage','$has_image','$is_profile_image','$is_cover_image')";

$DB = new Database();


$DB->save($query);

}else
{
$this->error .= "Please type something to post!<br>";
}

return $this->error;
}

public function get_posts($id)


{

$query = " select * from posts where userid = '$id' order by id desc limit 10 " ;

$DB = new Database();


$result = $DB->read($query);

if ($result)
{
return $result ;
}else
{

10 | P a g e
return false;
}
}

public function get_one_post($postid)


{
if (!is_numeric($postid)) {

return false;
}

$query = " select * from posts where postid = '$postid' limit 1" ;

$DB = new Database();


$result = $DB->read($query);

if ($result)
{
return $result[0] ;
}else
{
return false;
}
}

public function delete_post($postid)


{
if (!is_numeric($postid)) {

return false;
}

$query = " select * from posts where postid = '$postid' limit 1" ;

$DB = new Database();


$DB->save($query);

public function i_own_post($postid,$mybook_userid)


{
if (!is_numeric($postid)) {

return false;
}

$query = "select * from posts where postid = '$postid' limit 1" ;

$DB = new Database();


$result = $DB->read($query);

11 | P a g e
if (is_array($result)) {

if ($result[0]['userid'] == $mybook_userid) {

return true;
}
}

return false;
}

public function get_likes($id,$type){

$DB = new Database();

if ($type == "post" && is_numeric($id)) {

// get like details


$sql = "select likes from likes where type ='post' && contentid = '$id' limit 1 ";
$result = $DB->read($sql);
if (is_array($result)) {

$likes = json_decode($result[0]['likes'],true);
return $likes;
}
}

return false;
}

public function like_post($id,$type,$mybook_userid){

if ($type == "post") {

$DB = new Database();

// save like details


$sql = "select likes from likes where type ='post' && contentid = '$id' limit 1 ";
$result = $DB->read($sql);
if (is_array($result)) {

$likes = json_decode($result[0]['likes'],true);

$user_ids = array_column($likes, "userid");

if(!in_array($mybook_userid,$user_ids)) {

$arr["userid"] = $mybook_userid;
$arr["date"] = date("Y-m-d H:i:s");

$likes[] = $arr;

12 | P a g e
$likes_string = json_encode($likes);
$sql = "update likes set likes = '$likes_string' where type ='post' &&
contentid = '$id' limit 1";
$DB->save($sql);

// increment the posts table


$sql = "update posts set likes = likes + 1 where postid = '$id' limit 1 ";
$DB->save($sql);

}else{

$key = array_search($mybook_userid, $user_ids);


unset($likes[$key]);

$likes_string = json_encode($likes);
$sql = "update likes set likes = '$likes_string' where type ='post' &&
contentid = '$id' limit 1";
$DB->save($sql);

// increment the posts table


$sql = "update posts set likes = likes - 1 where postid = '$id' limit 1 ";
$DB->save($sql);
}

}else{

$arr["userid"] = $mybook_userid;
$arr["date"] = date("Y-m-d H:i:s");

$arr2[] = $arr;

$likes = json_encode($arr2);
$sql = "insert into likes (type,contentid,likes) values ('$type','$id','$likes')";
$DB->save($sql);

// increment the posts table


$sql = "update posts set likes = likes + 1 where postid = '$id' limit 1 ";
$DB->save($sql);
}
}
}

private function create_postid()


{

$length = rand(4,19);
$number = "";
for ($i=0; $i < $length ; $i++) {
# code...

13 | P a g e
$new_rand = rand(0,9);

$number = $number . $new_rand;


}

return $number;
}
}

14 | P a g e
Profile .php

<?php

class Profile
{

function get_profile($id){

$id = addslashes($id);
$DB = new Database();
$query = "select * from users where userid = '$id' limit 1";
return $DB->read($query);

}
}

user .php

15 | P a g e
<?php

class User
{

public function get_data($id)


{

$query= " select * from users where userid = $id limit 1";

$DB = new Database();


$result = $DB->read($query);

if ($result)
{

$row = $result[0];
return $row;
}else
{
return false;
}
}

public function get_user($id)


{

$query = "select * from users where userid = '$id' limit 1";


$DB = new Database();
$result = $DB->read($query);

if ($result)
{
return $result[0];
}else
{

return false;
}
}

public function get_friends($id)


{

$query = "select * from users where userid != '$id' ";


$DB = new Database();
$result = $DB->read($query);

if ($result)
{
return $result;

16 | P a g e
}else
{

return false;
}
}

image .php

<?php

17 | P a g e
class Image
{

public function generate_filename($length)


{

$array =
array(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','
C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$text = "";

for ($x = 0; $x < $length; $x++)


{

$random = rand(0,61);
$text .= $array[$random];
}

return $text;
}

public function crop_image($orignal_file_name,$cropped_file_name,$max_width,$max_height)


{

if (file_exists($orignal_file_name))
{

$orignal_image = imagecreatefromjpeg($orignal_file_name);

$orignal_width = imagesx($orignal_image);
$orignal_height = imagesy($orignal_image);

if ($orignal_height > $orignal_width)


{
//make width equal to max width;
$ratio = $max_width / $orignal_width;

$new_width = $max_width;
$new_height = $orignal_height * $ratio;

}else
{

// Make width equal to max width


$ratio = $max_height / $orignal_height;

$new_height = $max_height;
$new_width = $orignal_width * $ratio;
}
}

18 | P a g e
//adjust incase max width and height are diffrent
if($max_width != $max_height)
{

if($max_height > $max_width)


{

if($max_height > $new_height)


{
$adjustment = ($max_height / $new_height);
}else
{
$adjustment = ($new_height / $max_height);
}

$new_width = $new_width * $adjustment;


$new_height = $new_height * $adjustment;
}else
{

if($max_width > $new_width)


{
$adjustment = ($max_width / $new_width);
}else
{
$adjustment = ($new_width / $max_width);
}

$new_width = $new_width * $adjustment;


$new_height = $new_height * $adjustment;
}
}
//image adjustment for profile and cover image

if ($max_width != $max_height)
{
if ($max_height > $max_width)
{

if ($max_height > $new_height)


{
$adjustment = ($max_height / $new_height);
}else
{
$adjustment = ($max_height / $max_height);
}

$new_width = $new_width * $adjustment;


$new_height = $new_height * $adjustment;

}else

19 | P a g e
{
if ($max_width > $new_width)
{
$adjustment = ($max_width / $new_width);
}else
{
$adjustment = ($new_width / $max_width);
}

$new_width = $new_width * $adjustment;


$new_height = $new_height * $adjustment;

}
}

$new_image = imagecreatetruecolor($new_width, $new_height);


imagecopyresampled($new_image, $orignal_image, 0, 0, 0, 0, $new_width, $new_height,
$orignal_width, $orignal_height);

imagedestroy($orignal_image);

if ($max_width != $max_height)
{
if ($max_width > $max_height)
{

$diff = ($new_height - $max_height);


if ($diff < 0) {
$diff = $diff * -1;
}

$y = round($diff / 2);
$x = 0;
}else
{

$diff = ($new_width - $max_height);


if ($diff < 0) {
$diff = $diff * -1;
}
$x = round($diff / 2);
$y = 0;
}

}else
{
if ($new_height > $new_width)
{

$diff = ($new_height - $new_width);


$y = round($diff / 2);

20 | P a g e
$x = 0;
}else
{

$diff = ($new_width - $new_height);


$x = round($diff / 2);
$y = 0;
}
}

$new_cropped_image = imagecreatetruecolor($max_width, $max_height);


imagecopyresampled($new_cropped_image, $new_image, 0, 0, $x, $y, $max_width,
$max_height, $max_width, $max_height);

imagedestroy($new_image);

imagejpeg($new_cropped_image,$cropped_file_name, 90);
imagedestroy($new_cropped_image);
}

//resize the image


public function resize_image($orignal_file_name,$resized_file_name,$max_width,$max_height)
{

if (file_exists($orignal_file_name))
{

$orignal_image = imagecreatefromjpeg($orignal_file_name);

$orignal_width = imagesx($orignal_image);
$orignal_height = imagesx($orignal_image);

if ($orignal_height > $orignal_width)


{
//make width equal to max width;
$ratio = $max_width / $orignal_width;

$new_width = $max_width;
$new_height = $orignal_height * $ratio;

}else
{

// Make width equal to max width


$ratio = $max_height / $orignal_height;

$new_width = $max_height;
$new_height = $orignal_width * $ratio;

}
}

21 | P a g e
//image adjustment for profile and cover image

if ($max_width != $max_height)
{
if ($max_height > $max_width)
{

if ($max_height > $new_height)


{
$adjustment = ($max_height / $new_height);
}else
{
$adjustment = ($max_height / $max_height);
}

$new_width = $new_width * $adjustment;


$new_height = $new_height * $adjustment;

}else
{
if ($max_width > $new_width)
{
$adjustment = ($max_width / $new_width);
}else
{
$adjustment = ($new_width / $max_width);
}

$new_width = $new_width * $adjustment;


$new_height = $new_height * $adjustment;

}
}

$new_image = imagecreatetruecolor($new_width, $new_height);


imagecopyresampled($new_image, $orignal_image, 0, 0, 0, 0, $new_width, $new_height,
$orignal_width, $orignal_height);

imagedestroy($orignal_image);

imagejpeg($new_image,$resized_file_name, 90);
imagedestroy($new_image);
}

// create for thumbnails for cover image


public function get_thumb_cover($filename)
{

$thumbnail = $filename . "_cover_thumb.jpg";


if (file_exists($thumbnail))

22 | P a g e
{
return $thumbnail;
}

$this->crop_image($filename,$thumbnail,1366,488);

if (file_exists($thumbnail))
{
return $thumbnail;
}else
{
return $filename;
}
}

// create for thumbnails for profile image


public function get_thumb_profile($filename)
{

$thumbnail = $filename . "_profile_thumb.jpg";


if (file_exists($thumbnail))
{
return $thumbnail;
}

$this->crop_image($filename,$thumbnail,600,600 );

if (file_exists($thumbnail))
{
return $thumbnail;
}else
{
return $filename;
}
}

// create for thumbnails for post image


public function get_thumb_post($filename)
{

$thumbnail = $filename . "_post_thumb.jpg";


if (file_exists($thumbnail))
{
return $thumbnail;
}

$this->crop_image($filename,$thumbnail,600,600);

if (file_exists($thumbnail))
{
return $thumbnail;

23 | P a g e
}else
{
return $filename;
}
}
}

header .php

<?php

$corner_image = "images/user_male.jpg";
if (isset($USER)){
if(file_exists($USER['profile_image']))
{
$image_class = new Image();
$corner_image = $image_class->get_thumb_profile($USER['profile_image']);

24 | P a g e
}else{

if ($USER['gender'] == "Female") {

$corner_image = "images/user_female.jpg";
}
}
}
?>

<!-- Top Bar -->


<div id="blue_bar">
<div style="width: 800px;margin: auto;font-size: 30px;">

<a href="index.php" style="color: white;text-decoration: none;">Mybook </a>

&nbsp &nbsp <input type="text" id="search_box" placeholder="Search For People">

<img src="<?php echo $corner_image ?>" style= "width:50px; float: right;">

<a href="logout.php">
<span style="font-size: 13px; float: right;margin: 10px;color: white;">Logout</span>
</a>

</div>
</div>

delete .php

<?php

include("classes/autoload.php");

$login = new login();


$user_data = $login->check_login($_SESSION['mybook_userid']);

25 | P a g e
$Post = new Post();

$ERROR = "";
if (isset($_GET['id'])) {

$ROW = $Post->get_one_post($_GET['id']);

if (!$ROW ) {

$ERROR = "No such post was found ";


}else{

if ($ROW['userid'] != $_SESSION['mybook_userid']) {

$ERROR = " Access Denied!";


}
}

}else{

$ERROR = "No such post was found ";


}

// if something goes wrong


if ($_SERVER['REQUEST_METHOD'] == "POST") {

$Post->delete_post($_POST['postid']);
header("Location: profile.php");
die;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Delete | Mybook</title>
</head>

<style type="text/css">

#blue_bar{

height: 50px;
background-color: #405d9b;
color: #d9dfeb;

#search_box{

26 | P a g e
width: 400px;
height: 20px;
border-radius: 5px;
border: none;
padding: 4px;
font-size: 14px;
background-image: url(search.png);

#profile_pic{

width: 150px;
border-radius: 50%;
border: solid 2px white;
}

#menu_buttons{

width: 100px;
display: inline-block;
margin: 2px;
}

#friends_img{

width:75px;
float: left;
margin: 8px;

#friends_bar{

min-height: 400px;
margin-top: 20px;
padding: 8px;
text-align: center;
font-size: 20px;
color: #405d9b;
}

#friends{

clear: both;
font-size: 12px;
font-weight: bold;
color: #405d9b;
}

textarea{

27 | P a g e
width: 100%;
border: none;
font-family: tahoma;
font-size: 14px;
height: 60px;

#post_button{

float: right;
background-color: #405d9b;
border: none;
color: white;
padding: 4px;
font-size: 14px;
border-radius: 2px;
width: 50px;
}

#post_bar{

margin-top: 20px;
background-color: white;
padding: 10px;
}
}

</style>

<body style="font-family: tahoma; background-color: #d0d8e4;">

<br>
<!-- Top Bar -->
<?php include ("header.php"); ?>

<!-- Cover Area -->


<div style="width: 800px; margin: auto;min-height: 400px;">

<!-- Below Cover Area -->


<div style="display: flex;">

<!-- Posts Area -->


<div style="min-height: 400px;flex: 2.5;padding: 20px;padding-right:0px;">

<div style="border: solid thin #aaa; padding: 10px;background-color:


white;">

<form method="post">

28 | P a g e
<?php

if ($ERROR != "") {

echo $ERROR;
}else{

echo "Are you sure you want to


delete this post? <br><br>";

$user = new User();


$ROW_USER = $user-
>get_user($ROW['userid']);

include("post_delete.php");
echo "<input type='hidden'
name='postid' value='$ROW[postid]'>";
echo "<input id='post_button'
type='submit' value='Delete'>";
}
?>

<br>
</form>
</div>

</div>
</div>

</div>

</body>
</html>

5.3 Code Efficiency

Code efficiency is directly linked with algorithmic efficiency and the speed of runtime
execution for software. It is the key element in ensuring high performance. The goal of code
efficiency is to reduce resource consumption and completion time as much as possible with
minimum risk to business or operating environment.
One of the recommended best practices in coding is to ensure good code efficiency. Well developed
programming codes should be able to handle complex algorithms.

29 | P a g e
Recommendations for code efficiency include:

• To remove unnecessary code or code that goes to redundant processing


• To make use of optimal memory and nonvolatile storage
• To ensure the best speed or run time for completing the algorithm
• To make use of reusable components wherever possible
• To create programming code that ensures data integrity and consistency
• To develop programming code that's compliant with the design logic and flow
• To make use of coding practices applicable to the related software

5.4 Testing Approach

A test approach is the test strategy implementation of a project, defines how testing would be carried
out. Test approach has two techniques:

• Proactive - An approach in which the test design process is initiated as early as possible in
order to find and fix the defects before the build is created.
• Reactive - An approach in which the testing is not started until after design and coding are
completed.

Different Test approaches:


There are many strategies that a project can adopt depending on the context and some of them
are:

• Dynamic and heuristic approaches

• Consultative approaches

• Model-based approach that uses statistical information about failure rates.

• Approaches based on risk-based testing where the entire development takes place based on
the risk

• Methodical approach, which is based on failures.

• Standard-compliant approach specified by industry-specific standards.

Factors to be considered:
• Risks of product or risk of failure or the environment and the company.

• Expertise and experience of the people in the proposed tools and techniques.

• Regulatory and legal aspects, such as external and internal regulations of the development
process.

30 | P a g e
• The nature of the product and the domain

5.4.1 Unit Testing

UNIT TESTING is a level of software testing where individual units/ components of a


software are tested. The purpose is to validate that each unit of the software performs as
designed. A unit is the smallest testable part of any software. It usually has one or a few
inputs and usually a single output

Unit Testing Benefits

• Unit testing increases confidence in changing/ maintaining code.


• Codes are more reusable. In order to make unit testing possible, codes need to be modular.
This means that codes are easier to reuse.
• Development is faster. How? If you do not have unit testing in place, you write your code and
perform that fuzzy ‘developer test’
• The cost of fixing a defect detected during unit testing is lesser in comparison to that of
defects detected at higher levels.
• Debugging is easy. When a test fails, only the latest changes need to be debugged.

Unit Testing Techniques:

• Black-Box Testing- Using which the user interface, input and output are tested.
• White-Box Testing- used to test each one of those functions behavior is tested.
• Grey-Box Testing- Used to execute tests, risks and assessment method.

5.4.2 Integration Testing


INTEGRATION TESTING is a level of software testing where individual units are combined and
tested as a group.
Integration Strategies

• Big bang Approach: Big Bang Integration Testing is an integration testing strategy
wherein all units are linked at once, resulting in a complete system

31 | P a g e
• Top down Integration: Top-down integration testing is an integration testing
technique used in order to simulate the behavior of the lower-level modules that are
not yet integrated.
• Bottom up Integration: Bottom-up testing is an approach to integrated testing where
the lowest level components are tested first, then used to facilitate the testing of
higher level components.
• Hybrid Integration: A hybrid integration platform is a combination of on-premise
and cloud based system, securely connected using a technology like Transport Layer
Security, so that it supports the integration of on-premise endpoints, cloud endpoints
and the combination of the two, for all integration patterns.

32 | P a g e
RESULTS AND DISCUSSIONS

33 | P a g e
65 | P a g e

Chapter 6

Results and Discussion


• 6.1 Test Reports

34 | P a g e
35 | P a g e
36 | P a g e
37 | P a g e
38 | P a g e
39 | P a g e
40 | P a g e
41 | P a g e
42 | P a g e
43 | P a g e
44 | P a g e
CONCLUSION

45 | P a g e
Chapter 7

Conclusion
7.1 Conclusion
Basically we can have a smooth and easy interaction on website where people can make
friends online and have great social network.
Another benefit is it is a great platform where people from all over the globe can
connect share their ideas and culture and ofcourse make friends online and grow their
social network.

7.2 Limitation
Still on growing phase. updates to be added.

7.3 Future Scope


Will add messages and e-commerce to it.

7.4 Reference

1) You Tube Chanel : Quick Programming

46 | P a g e

You might also like