/** * Create new comment. This function is used by ProjectForms to post comments * to the messages * * @param string $content * @param boolean $is_private * @return Comment or NULL if we fail to save comment * @throws DAOValidationError */ function addComment($content, $is_private = false) { $comment = new Comment(); $comment->setText($content); $comment->setIsPrivate($is_private); return $this->attachComment($comment); }
/** * @return array */ public static function loadAll() { $dbWrapper = new DatabaseWrapper(); $connection = $dbWrapper->getConnection(); $query = "SELECT * FROM temos;"; $temos = []; foreach ($connection->query($query) as $row) { $tema = new Tema($connection); $tema->setId($row['id']); $tema->setDate($row['subject_date']); $tema->setName($row['name']); $query = 'SELECT * FROM comments INNER JOIN temos ON comments.subjectId = ' . $row['id'] . " AND temos.id = " . $row['id'] . ";"; $comments = []; foreach ($connection->query($query) as $i) { $comment = new Comment($connection); $comment->setId($i['id']); $comment->setsubjectId($i['subjectId']); $comment->setText($i['text']); $comment->setDate($i['date']); $comment->setAuthor($i['author']); $comments[] = $comment; } foreach ($comments as $comment) { $tema->setComments($comment); } $temos[] = $tema; } return $temos; }
/** * Adds a new comment to an article * * @param int $articleId * @param string $commentTitle * @param string $commentText * @return Comment */ public function addComment($articleId, $commentTitle, $commentText) { $comment = new Comment(); $comment->setTitle($commentTitle); $comment->setText($commentText); $this->entityManager->persist($comment); $comment->setArticle($this->find($articleId)); $this->entityManager->flush(); return $comment; }
public function testGetSet() { $comment = new Comment('/* Some comment */', 1); $this->assertSame('/* Some comment */', $comment->getText()); $this->assertSame('/* Some comment */', (string) $comment); $this->assertSame(1, $comment->getLine()); $comment->setText('/* Some other comment */'); $comment->setLine(10); $this->assertSame('/* Some other comment */', $comment->getText()); $this->assertSame('/* Some other comment */', (string) $comment); $this->assertSame(10, $comment->getLine()); }
public function getAllBySubjectId($subId) { $query = "SELECT * FROM comments WHERE subjectId = {$subId}"; $comments = []; foreach ($this->connection->query($query) as $row) { $comment = new Comment(); $comment->setSubjectId($row['subjectId']); $comment->setDate($row['date']); $comment->setText($row['text']); $comment->setAuthor($row['author']); $comments[] = $comment; } return $comments; }
public function getModel() { $model = new Comment(); $model->setText($this->_data['text']); $model->setStatementId($this->_st->getId()); if ($this->_user->user_id) { $model->setUserId($this->_user->user_id); $model->setUsername($this->_user->name); } else { $model->setUserId(0); $model->setUsername($this->_data['name']); $model->setEmail($this->_data['email']); } $model->setIpAddress($this->_user->ip_address); return $model; }
public function getComment($selectResult) { $comment = new Comment(); while ($list = mysqli_fetch_assoc($selectResult)) { $comment->setId($list['com_comment_id']); $comment->setText($list['com_text']); $comment->setRatingId($list['com_rating_id']); $comment->setCreatedBy($list['com_created_by']); $comment->setEntryId($list['com_entry_id']); } //end while return $comment; }
<?php require_once 'Comment.php'; require_once '../databaseWrapper.php'; $dbWrapper = new DatabaseWrapper(); $connection = $dbWrapper->getConnection(); $ids = []; foreach ($connection->query('SELECT id FROM temos') as $row) { $ids[] = $row['id']; } $text = "Lambada lambada lambada"; $comment = new Comment($connection); $comment->setAuthor('Anonymous'); $comment->setSubjectID($ids[rand(0, sizeof($ids) - 1)]); $comment->setText($text); $comment->save(); header('Location: Controller.php');
function cloneTask($new_st_date = '', $new_due_date = '', $copy_status = false, $copy_repeat_options = true, $parent_subtask = 0) { $new_task = new TemplateTask(); if ($parent_subtask != 0) { $new_task->setParentId($parent_subtask); } else { $new_task->setParentId($this->getParentId()); } $new_task->setObjectName($this->getObjectName()); $new_task->setText($this->getText()); $new_task->setAssignedToContactId($this->getAssignedToContactId()); $new_task->setAssignedOn($this->getAssignedOn()); $new_task->setAssignedById($this->getAssignedById()); $new_task->setTimeEstimate($this->getTimeEstimate()); $new_task->setStartedOn($this->getStartedOn()); $new_task->setStartedById($this->getStartedById()); $new_task->setPriority($this->getPriority()); $new_task->setState($this->getState()); $new_task->setOrder($this->getOrder()); $new_task->setMilestoneId($this->getMilestoneId()); $new_task->setFromTemplateId($this->getFromTemplateId()); $new_task->setUseStartTime($this->getUseStartTime()); $new_task->setUseDueTime($this->getUseDueTime()); $new_task->setTypeContent($this->getTypeContent()); if ($this->getParentId() == 0) { //if not subtask if ($this->getOriginalTaskId() == 0) { $new_task->setOriginalTaskId($this->getObjectId()); } else { $new_task->setOriginalTaskId($this->getOriginalTaskId()); } } if ($this->getDueDate() instanceof DateTimeValue) { $new_task->setDueDate(new DateTimeValue($this->getDueDate()->getTimestamp())); } if ($this->getStartDate() instanceof DateTimeValue) { $new_task->setStartDate(new DateTimeValue($this->getStartDate()->getTimestamp())); } if ($copy_status) { $new_task->setCompletedById($this->getCompletedById()); $new_task->setCompletedOn($this->getCompletedOn()); } if ($copy_repeat_options) { $new_task->setRepeatEnd($this->getRepeatEnd()); $new_task->setRepeatForever($this->getRepeatForever()); $new_task->setRepeatNum($this->getRepeatNum()); $new_task->setRepeatBy($this->getRepeatBy()); $new_task->setRepeatD($this->getRepeatD()); $new_task->setRepeatM($this->getRepeatM()); $new_task->setRepeatY($this->getRepeatY()); } if ($new_st_date != "") { if ($new_task->getStartDate() instanceof DateTimeValue) { $new_task->setStartDate($new_st_date); } } if ($new_due_date != "") { if ($new_task->getDueDate() instanceof DateTimeValue) { $new_task->setDueDate($new_due_date); } } $new_task->save(); if (is_array($this->getAllLinkedObjects())) { foreach ($this->getAllLinkedObjects() as $lo) { $new_task->linkObject($lo); } } $sub_tasks = $this->getAllSubTasks(); foreach ($sub_tasks as $st) { $new_dates = $this->getNextRepetitionDatesSubtask($st, $new_task, $new_st_date, $new_due_date); if ($st->getParentId() == $this->getId()) { $new_st = $st->cloneTask(array_var($new_dates, 'st'), array_var($new_dates, 'due'), $copy_status, $copy_repeat_options, $new_task->getId()); if ($copy_status) { $new_st->setCompletedById($st->getCompletedById()); $new_st->setCompletedOn($st->getCompletedOn()); $new_st->save(); } $new_task->attachTask($new_st); } } foreach ($this->getAllComments() as $com) { $new_com = new Comment(); $new_com->setAuthorEmail($com->getAuthorEmail()); $new_com->setAuthorName($com->getAuthorName()); $new_com->setAuthorHomepage($com->getAuthorHomepage()); $new_com->setCreatedById($com->getCreatedById()); $new_com->setCreatedOn($com->getCreatedOn()); $new_com->setUpdatedById($com->getUpdatedById()); $new_com->setUpdatedOn($com->getUpdatedOn()); $new_com->setText($com->getText()); $new_com->setRelObjectId($new_task->getId()); $new_com->save(); } $_POST['subscribers'] = array(); foreach ($this->getSubscribers() as $sub) { $_POST['subscribers']["user_" . $sub->getId()] = "checked"; } $obj_controller = new ObjectController(); $obj_controller->add_to_members($new_task, $this->getMemberIds()); $obj_controller->add_subscribers($new_task); foreach ($this->getCustomProperties() as $prop) { $new_prop = new ObjectProperty(); $new_prop->setRelObjectId($new_task->getId()); $new_prop->setPropertyName($prop->getPropertyName()); $new_prop->setPropertyValue($prop->getPropertyValue()); $new_prop->save(); } $custom_props = CustomProperties::getAllCustomPropertiesByObjectType("TemplateTasks"); foreach ($custom_props as $c_prop) { $values = CustomPropertyValues::getCustomPropertyValues($this->getId(), $c_prop->getId()); if (is_array($values)) { foreach ($values as $val) { $cp = new CustomPropertyValue(); $cp->setObjectId($new_task->getId()); $cp->setCustomPropertyId($val->getCustomPropertyId()); $cp->setValue($val->getValue()); $cp->save(); } } } $reminders = ObjectReminders::getByObject($this); foreach ($reminders as $reminder) { $copy_reminder = new ObjectReminder(); $copy_reminder->setContext($reminder->getContext()); $reminder_date = $new_task->getColumnValue($reminder->getContext()); if ($reminder_date instanceof DateTimeValue) { $reminder_date = new DateTimeValue($reminder_date->getTimestamp()); $reminder_date->add('m', -$reminder->getMinutesBefore()); } $copy_reminder->setDate($reminder_date); $copy_reminder->setMinutesBefore($reminder->getMinutesBefore()); $copy_reminder->setObject($new_task); $copy_reminder->setType($reminder->getType()); $copy_reminder->setUserId($reminder->getUserId()); $copy_reminder->save(); } return $new_task; }
function cloneTask($copy_status = false) { $new_task = new ProjectTask(); $new_task->setParentId($this->getParentId()); $new_task->setTitle($this->getTitle()); $new_task->setText($this->getText()); $new_task->setAssignedToCompanyId($this->getAssignedToCompanyId()); $new_task->setAssignedToUserId($this->getAssignedToUserId()); $new_task->setAssignedOn($this->getAssignedOn()); $new_task->setAssignedById($this->getAssignedById()); $new_task->setTimeEstimate($this->getTimeEstimate()); $new_task->setStartedOn($this->getStartedOn()); $new_task->setStartedById($this->getStartedById()); $new_task->setPriority($this->getPriority()); $new_task->setState($this->getState()); $new_task->setOrder($this->getOrder()); $new_task->setMilestoneId($this->getMilestoneId()); $new_task->setIsPrivate($this->getIsPrivate()); $new_task->setIsTemplate($this->getIsTemplate()); $new_task->setFromTemplateId($this->getFromTemplateId()); if ($this->getDueDate() instanceof DateTimeValue) { $new_task->setDueDate(new DateTimeValue($this->getDueDate()->getTimestamp())); } if ($this->getStartDate() instanceof DateTimeValue) { $new_task->setStartDate(new DateTimeValue($this->getStartDate()->getTimestamp())); } if ($copy_status) { $new_task->setCompletedById($this->getCompletedById()); $new_task->setCompletedOn($this->getCompletedOn()); } $new_task->save(); $new_task->setTagsFromCSV(implode(",", $this->getTagNames())); foreach ($this->getWorkspaces() as $ws) { $new_task->addToWorkspace($ws); } if (is_array($this->getAllLinkedObjects())) { foreach ($this->getAllLinkedObjects() as $lo) { $new_task->linkObject($lo); } } $sub_tasks = $this->getAllSubTasks(); foreach ($sub_tasks as $st) { if ($st->getParentId() == $this->getId()) { $new_st = $st->cloneTask($copy_status); if ($copy_status) { $new_st->setCompletedById($st->getCompletedById()); $new_st->setCompletedOn($st->getCompletedOn()); $new_st->save(); } $new_task->attachTask($new_st); } } foreach ($this->getAllComments() as $com) { $new_com = new Comment(); $new_com->setAuthorEmail($com->getAuthorEmail()); $new_com->setAuthorName($com->getAuthorName()); $new_com->setAuthorHomepage($com->getAuthorHomepage()); $new_com->setCreatedById($com->getCreatedById()); $new_com->setCreatedOn($com->getCreatedOn()); $new_com->setUpdatedById($com->getUpdatedById()); $new_com->setUpdatedOn($com->getUpdatedOn()); $new_com->setIsAnonymous($com->getIsAnonymous()); $new_com->setIsPrivate($com->getIsPrivate()); $new_com->setText($com->getText()); $new_com->setRelObjectId($new_task->getId()); $new_com->setRelObjectManager("ProjectTasks"); $new_com->save(); } $_POST['subscribers'] = array(); foreach ($this->getSubscribers() as $sub) { $_POST['subscribers']["user_" . $sub->getId()] = "checked"; } $obj_controller = new ObjectController(); $obj_controller->add_subscribers($new_task); foreach ($this->getCustomProperties() as $prop) { $new_prop = new ObjectProperty(); $new_prop->setRelObjectId($new_task->getId()); $new_prop->setRelObjectManager($prop->getRelObjectManager()); $new_prop->setPropertyName($prop->getPropertyName()); $new_prop->setPropertyValue($prop->getPropertyValue()); $new_prop->save(); } $custom_props = CustomProperties::getAllCustomPropertiesByObjectType("ProjectTasks"); foreach ($custom_props as $c_prop) { $values = CustomPropertyValues::getCustomPropertyValues($this->getId(), $c_prop->getId()); if (is_array($values)) { foreach ($values as $val) { $cp = new CustomPropertyValue(); $cp->setObjectId($new_task->getId()); $cp->setCustomPropertyId($val->getCustomPropertyId()); $cp->setValue($val->getValue()); $cp->save(); } } } $reminders = ObjectReminders::getByObject($this); foreach ($reminders as $reminder) { $copy_reminder = new ObjectReminder(); $copy_reminder->setContext($reminder->getContext()); $reminder_date = $new_task->getColumnValue($reminder->getContext()); if ($reminder_date instanceof DateTimeValue) { $reminder_date = new DateTimeValue($reminder_date->getTimestamp()); $reminder_date->add('m', -$reminder->getMinutesBefore()); } $copy_reminder->setDate($reminder_date); $copy_reminder->setMinutesBefore($reminder->getMinutesBefore()); $copy_reminder->setObject($new_task); $copy_reminder->setType($reminder->getType()); $copy_reminder->setUserId($reminder->getUserId()); $copy_reminder->save(); } return $new_task; }
require_once '../resources/require.php'; // Umożliwia wyśletanie odświeżonej treści strony, po dodaniu komentarza if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_GET['tweet_text'] = $_POST['tweet_text']; } // Tworzy obiekt tweet potrzebny do wyświetlania treści wpisu, który przekierował na tę stronę i dodawania do niego komentarzy $tweet = new Tweet($mysqli); $tweet->setText($_GET['tweet_text']); $tweet->loadFromDB(); // Tworzy nowy komentarz i uzupełnia treść informacji o sukcesie/błędach if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (trim($_POST['comment_text']) != '') { $comment = new Comment($mysqli); $comment->setTweetId($tweet->getId()); $comment->setUserId($_SESSION['user_id']); $comment->setText($_POST['comment_text']); $comment->setCreationDate(date("Y-m-d H:i:s")); if (!$comment->create()) { $info = 'Błąd przy dodawaniu komentarza.'; } else { $info = 'Dodano komentarz.'; } } else { $info = 'Uzupełnij treść komentarza.'; } } ?> <!DOCTYPE html> <html lang="pl-PL"> <title>Twitter | Wpisy</title>
SmartTest::instance()->progress(); } SmartTest::instance()->canwe = "countRelated"; R::gen("Blog,Comment"); $blog = new Blog(); $blog2 = new Blog(); $blog->setTitle("blog1"); $blog2->setTitle("blog2"); for ($i = 0; $i < 5; $i++) { $comment = new Comment(); $comment->setText("comment no. {$i} "); $blog->add($comment); } for ($i = 0; $i < 3; $i++) { $comment = new Comment(); $comment->setText("comment no. {$i} "); $blog2->add($comment); } if ($blog->numofComment() !== 5) { die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe); } else { SmartTest::instance()->progress(); } if ($blog2->numofComment() !== 3) { die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe); } else { SmartTest::instance()->progress(); } SmartTest::instance()->canwe = "associate tables of the same name"; $blog = new Blog(); $blogb = new Blog();
public function loadAllComments() { $tweetId = $this->id; $conn = $this->connection; $commentsArray = []; $sqlQuery = "SELECT user_id, text FROM comments WHERE tweet_id = {$tweetId} ORDER BY created_at DESC"; $result = $conn->query($sqlQuery); if ($result->num_rows > 0) { while (list($userId, $text) = $result->fetch_array(MYSQLI_NUM)) { $comment = new Comment($conn); $comment->setUserId($userId); $comment->setText($text); $commentsArray[] = $comment; } return $commentsArray; } else { return false; } }
/** * Copies related data from an object to another (members, linked_objects, custom_properties, subscribers, reminders and comments) * @param $object: Original object to copy data * @param $copy: Object to be modified with the data of the $orignal object * @param $options: set which type of data will not be copied */ function copy_additional_object_data($object, &$copy, $options = array()) { if (!$object instanceof ContentDataObject || !$copy instanceof ContentDataObject) { // if not valid objects return return; } $copy_members = !array_var($options, 'dont_copy_members'); $copy_linked_objects = !array_var($options, 'dont_copy_linked_objects'); $copy_custom_properties = !array_var($options, 'dont_copy_custom_properties'); $copy_subscribers = !array_var($options, 'dont_copy_subscribers'); $copy_reminders = !array_var($options, 'dont_copy_reminders'); $copy_comments = !array_var($options, 'dont_copy_comments'); $controller = new ObjectController(); // copy members if ($copy_members) { $object_members = $object->getMembers(); $copy->addToMembers($object_members); Hook::fire('after_add_to_members', $copy, $object_members); $copy->addToSharingTable(); } // copy linked objects if ($copy_linked_objects) { $copy->copyLinkedObjectsFrom($object); } // copy custom properties if ($copy_custom_properties) { // custom properties defined in "settings" $cp_object_type_id = $object->getObjectTypeId(); if ($object instanceof TemplateTask || $object instanceof TemplateMilestone) { $cp_object_type_id = $copy->getObjectTypeId(); } $custom_props = CustomProperties::getAllCustomPropertiesByObjectType($cp_object_type_id); foreach ($custom_props as $c_prop) { $values = CustomPropertyValues::getCustomPropertyValues($object->getId(), $c_prop->getId()); if (is_array($values)) { foreach ($values as $val) { $cp = new CustomPropertyValue(); $cp->setObjectId($copy->getId()); $cp->setCustomPropertyId($val->getCustomPropertyId()); $cp->setValue($val->getValue()); $cp->save(); } } } // object properties (key-value) $copy->copyCustomPropertiesFrom($object); } // copy subscribers if ($copy_subscribers) { $subscribers_array = array(); foreach ($object->getSubscriberIds() as $user_id) { $subscribers_array["user_" . $user_id] = "1"; } $controller->add_subscribers($copy, $subscribers_array); } // copy reminders if ($copy_reminders) { $reminders = ObjectReminders::getByObject($object); foreach ($reminders as $reminder) { $copy_reminder = new ObjectReminder(); $copy_reminder->setContext($reminder->getContext()); $reminder_date = $copy->getColumnValue($reminder->getContext()); if ($reminder_date instanceof DateTimeValue) { $reminder_date = new DateTimeValue($reminder_date->getTimestamp()); $reminder_date->add('m', -$reminder->getMinutesBefore()); } $copy_reminder->setDate($reminder_date); $copy_reminder->setMinutesBefore($reminder->getMinutesBefore()); $copy_reminder->setObject($copy); $copy_reminder->setType($reminder->getType()); $copy_reminder->setUserId($reminder->getUserId()); $copy_reminder->save(); } } // copy comments if ($copy_comments) { foreach ($object->getAllComments() as $com) { $new_com = new Comment(); $new_com->setAuthorEmail($com->getAuthorEmail()); $new_com->setAuthorName($com->getAuthorName()); $new_com->setAuthorHomepage($com->getAuthorHomepage()); $new_com->setCreatedById($com->getCreatedById()); $new_com->setCreatedOn($com->getCreatedOn()); $new_com->setUpdatedById($com->getUpdatedById()); $new_com->setUpdatedOn($com->getUpdatedOn()); $new_com->setText($com->getText()); $new_com->setRelObjectId($copy->getId()); $new_com->save(); } } }
<?php include_once "../configRoot.php"; require_once ROOT_NAME . "/classes/Database.php"; require_once ROOT_NAME . "/classes/Comment.php"; require_once ROOT_NAME . "/includes/checkProfile.php"; if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["comment"])) { $userId = $id; // user's id from a cookie $page = $_POST["page"]; $commentText = $_POST["comment"]; $commentText = htmlentities($commentText); $tweetId = $_POST["tweet_id"]; $db = Database::getInstance(); $conn = $db->getConnection(); $comment = new Comment($conn); $comment->setUserId($id); $comment->setText($commentText); $comment->setTweetId($tweetId); $comment->addToDatabase(); if (strpos($page, "tweet")) { header("location: ../{$page}"); } elseif (strpos($page, "user")) { header("location: ../{$page}"); } else { header("location: ../index.php"); } } else { header("location: ../index.php"); }
if ($_POST) { $comment_text = isset($_POST['newComment']) && $_POST['newComment'] != 'undefined' ? $_POST['newComment'] : ""; $comment_entity_id = isset($_POST['commentEntityId']) && $_POST['commentEntityId'] != 'undefined' ? $_POST['commentEntityId'] : ""; if ($comment_text != "" && $comment_entity_id != "") { $comment_text = rawurldecode($comment_text); //decode the url //handle badword $bw_handler = new BadwordManager(); $bw_list = $bw_handler->getBadWordList(); //print_r($bw_list); $replacement = $bw_handler->getReplacementList(); //print_r($replacement); $filtered_comment_text = preg_replace($bw_list, $replacement, $comment_text); //echo "new comment filtered comment: ".$filtered_comment_text; $new_comment = new Comment(); $new_comment->setText($filtered_comment_text); $new_comment->setCreatedBy($user->getUserId()); $new_comment->setRatingId(''); $com_entry_id = $comment_entity_id; //$com_entry_id = 'eng2'; $new_comment->setEntryId($com_entry_id); $commentManager = new CommentManager(); $new_comment_id = $commentManager->addComment($new_comment); } } if ($new_comment_id > 0) { echo "Adding a new comment succeeded."; } else { echo "Adding a new comment failed."; } }
function testsperengine() { global $tests; SmartTest::instance()->progress(); SmartTest::instance()->testPack = "perform generic bean manipulation"; $ok = 1; $bean = RedBean_OODB::dispense("note"); $bean->message = "hai"; $bean->color = 3; $bean->date = time(); $bean->special = 'n'; $bean->state = 90; RedBean_OODB::set($bean); $bean2 = RedBean_OODB::getById("note", 1); if ($bean2->state != 90 || $bean2->special != 'n' || $bean2->message != 'hai') { $ok = 0; SmartTest::failedTest(); } SmartTest::instance()->progress(); $bean->message = "lorem ipsum"; RedBean_OODB::set($bean); $bean->message = 1; $bean->color = "green"; $bean->date = str_repeat("BLABLA", 100); RedBean_OODB::set($bean); $note = $bean; SmartTest::instance()->progress(); $person = RedBean_OODB::dispense("person"); $person->age = 50; $person->name = "Bob"; $person->gender = "m"; RedBean_OODB::set($person); RedBean_OODB::associate($person, $note); $memo = RedBean_OODB::getById("note", 1); $authors = RedBean_OODB::getAssoc($memo, "person"); if (count($authors) !== 1) { SmartTest::failedTest(); } RedBean_OODB::trash($authors[1]); $authors = RedBean_OODB::getAssoc($memo, "person"); if (count($authors) > 0) { $ok = 0; } if (!$ok) { SmartTest::failedTest(); } //unit tests //drop the note table SmartTest::instance()->progress(); SmartTest::instance()->testPack = "dispense an RedBean_OODB Bean"; $oBean = RedBean_OODB::dispense(); if (!$oBean instanceof OODBBean) { SmartTest::failedTest(); } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "put a bean in the database"; $person = RedBean_OODB::dispense("person"); $person->name = "John"; $person->age = 35; $person->gender = "m"; $person->hasJob = true; $id = RedBean_OODB::set($person); $johnid = $id; $person2 = RedBean_OODB::getById("person", $id); if ($person2->age != $person->age) { SmartTest::failedTest(); } $person2->anotherprop = 2; RedBean_OODB::set($person2); $person = RedBean_OODB::dispense("person"); $person->name = "Bob"; $person->age = 50; $person->gender = "m"; $person->hasJob = false; $bobid = RedBean_OODB::set($person); SmartTest::instance()->progress(); SmartTest::instance()->testPack = "find records on basis of similarity"; $ids = RedBean_OODB::getBySQL("`gender`={gender} order by `name` asc", array("gender" => "m"), "person"); if (count($ids) != 2) { SmartTest::failedTest(); } SmartTest::instance()->progress(); $ids = RedBean_OODB::getBySQL("`gender`={gender} OR `color`={clr} ", array("gender" => "m", "clr" => "red"), "person"); if (count($ids) != 0) { SmartTest::failedTest(); } SmartTest::instance()->progress(); $ids = RedBean_OODB::getBySQL("`gender`={gender} AND `color`={clr} ", array("gender" => "m", "clr" => "red"), "person"); if (count($ids) != 0) { SmartTest::failedTest(); } SmartTest::instance()->progress(); R::gen("Person"); $dummy = new Person(); $dummy->age = 40; SmartTest::instance()->test(count(Person::find($dummy, array("age" => ">"))), 1); $dummy->age = 20; SmartTest::instance()->test(count(Person::find($dummy, array("age" => ">"))), 2); $dummy->age = 100; SmartTest::instance()->test(count(Person::find($dummy, array("age" => ">"))), 0); $dummy->age = 100; SmartTest::instance()->test(count(Person::find($dummy, array("age" => "<="))), 2); $dummy->name = "ob"; SmartTest::instance()->test(count(Person::find($dummy, array("name" => "LIKE"))), 1); $dummy->name = "o"; SmartTest::instance()->test(count(Person::find($dummy, array("name" => "LIKE"))), 2); $dummy->gender = "m"; SmartTest::instance()->test(count(Person::find($dummy, array("gender" => "="))), 2); $dummy->gender = "f"; SmartTest::instance()->test(count(Person::find($dummy, array("gender" => "="))), 0); SmartTest::instance()->test(count(Person::listAll()), 2); SmartTest::instance()->test(count(Person::listAll(0, 1)), 1); SmartTest::instance()->test(count(Person::listAll(1)), 1); $can = Person::where("`gender`={gender} order by `name` asc", array("gender" => "m"), "person"); //test array access foreach ($can as $item) { if ($item->getName() == "Bob" || $item->getName() == "John") { SmartTest::instance()->progress(); } else { SmartTest::failedTest(); } } //test array access $bean = $can[0]; if ($bean->name == "Bob") { SmartTest::instance()->progress(); } else { SmartTest::failedTest(); } if ($can->count() != 2) { SmartTest::failedTest(); } SmartTest::instance()->progress(); $can->rewind(); SmartTest::instance()->test($can->key(), 0); SmartTest::instance()->test($can->valid(), true); SmartTest::instance()->test($can->current()->getName(), "Bob"); $can->next(); SmartTest::instance()->test($can->key(), 1); SmartTest::instance()->test($can->valid(), false); SmartTest::instance()->test($can->current()->getName(), "John"); $can->seek(0); SmartTest::instance()->test($can->key(), 0); $beans = $can->getBeans(); if (count($beans) != 2) { SmartTest::failedTest(); } SmartTest::instance()->progress(); //test slicing $can->slice(0, 1); $can->rewind(); SmartTest::instance()->test($can->current()->getName(), "Bob"); SmartTest::instance()->test($can->count(), 1); $b1 = array_shift($beans); if ($b1->name != "Bob") { SmartTest::failedTest(); } SmartTest::instance()->progress(); //basic functionality where() $beans = Person::where("`gender`={gender} order by `name` asc", array("gender" => "m"), "person")->getBeans(); if (count($beans) != 2) { SmartTest::failedTest(); } SmartTest::instance()->progress(); //without backticks should still work $beans = Person::where("gender={person} order by `name` asc", array("person" => "m"), "person")->getBeans(); if (count($beans) != 2) { SmartTest::failedTest(); } SmartTest::instance()->progress(); //like comparing should still work $beans = Person::where("gender={gender} and `name` LIKE {name} order by `name` asc", array("gender" => "m", "name" => "B%"), "person")->getBeans(); if (count($beans) != 1) { SmartTest::failedTest(); } SmartTest::instance()->progress(); $searchBean = RedBean_OODB::dispense("person"); $searchBean->gender = "m"; SmartTest::instance()->progress(); SmartTest::instance()->testPack = "associate beans with eachother?"; $app = RedBean_OODB::dispense("appointment"); $app->kind = "dentist"; RedBean_OODB::set($app); RedBean_OODB::associate($person2, $app); $appforbob = array_shift(RedBean_OODB::getAssoc($person2, "appointment")); if (!$appforbob || $appforbob->kind != "dentist") { SmartTest::failedTest(); } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "delete a bean?"; $person = RedBean_OODB::getById("person", $bobid); RedBean_OODB::trash($person); try { $person = RedBean_OODB::getById("person", $bobid); $ok = 0; } catch (ExceptionFailedAccessBean $e) { $ok = true; } if (!$ok) { SmartTest::failedTest(); } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "unassociate two beans?"; $john = RedBean_OODB::getById("person", $johnid); //hmmmmmm gaat mis bij innodb $app = RedBean_OODB::getById("appointment", 1); RedBean_OODB::unassociate($john, $app); $john2 = RedBean_OODB::getById("person", $johnid); $appsforjohn = RedBean_OODB::getAssoc($john2, "appointment"); if (count($appsforjohn) > 0) { SmartTest::failedTest(); } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "unassociate by deleting a bean?"; $anotherdrink = RedBean_OODB::dispense("whisky"); $anotherdrink->name = "bowmore"; $anotherdrink->age = 18; $anotherdrink->singlemalt = 'y'; RedBean_OODB::set($anotherdrink); RedBean_OODB::associate($anotherdrink, $john); $hisdrinks = RedBean_OODB::getAssoc($john, "whisky"); if (count($hisdrinks) !== 1) { SmartTest::failedTest(); } RedBean_OODB::trash($anotherdrink); $hisdrinks = RedBean_OODB::getAssoc($john, "whisky"); if (count($hisdrinks) !== 0) { SmartTest::failedTest(); } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "create parent child relationships?"; $pete = RedBean_OODB::dispense("person"); $pete->age = 48; $pete->gender = "m"; $pete->name = "Pete"; $peteid = RedBean_OODB::set($pete); $rob = RedBean_OODB::dispense("person"); $rob->age = 19; $rob->name = "Rob"; $rob->gender = "m"; $saskia = RedBean_OODB::dispense("person"); $saskia->age = 20; $saskia->name = "Saskia"; $saskia->gender = "f"; RedBean_OODB::set($saskia); RedBean_OODB::set($rob); RedBean_OODB::addChild($pete, $rob); RedBean_OODB::addChild($pete, $saskia); $children = RedBean_OODB::getChildren($pete); $names = 0; if (is_array($children) && count($children) === 2) { foreach ($children as $child) { if ($child->name === "Rob") { $names++; } if ($child->name === "Saskia") { $names++; } } } if (!$names) { SmartTest::failedTest(); } $daddies = RedBean_OODB::getParent($saskia); $daddy = array_pop($daddies); if ($daddy->name === "Pete") { $ok = 1; } else { $ok = 0; } if (!$ok) { SmartTest::failedTest(); } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "remove a child from a parent-child tree?"; RedBean_OODB::removeChild($daddy, $saskia); $children = RedBean_OODB::getChildren($pete); $ok = 0; if (count($children) === 1) { $only = array_pop($children); if ($only->name === "Rob") { $ok = 1; } } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "save on the fly while associating?"; $food = RedBean_OODB::dispense("dish"); $food->name = "pizza"; RedBean_OODB::associate($food, $pete); $petesfood = RedBean_OODB::getAssoc($pete, "food"); if (is_array($petesfood) && count($petesfood) === 1) { $ok = 1; } if (!$ok) { SmartTest::failedTest(); } //some extra tests... quick without further notice. $food = RedBean_OODB::dispense("dish"); $food->name = "spaghetti"; RedBean_OODB::trash($food); //test aggregation functions //insert stat table $s = RedBean_OODB::dispense("stattest"); $s->amount = 1; RedBean_OODB::set($s); $s = RedBean_OODB::dispense("stattest"); $s->amount = 2; RedBean_OODB::set($s); $s = RedBean_OODB::dispense("stattest"); $s->amount = 3; RedBean_OODB::set($s); SmartTest::instance()->testPack = "can we use aggr functions using Redbean?"; if (RedBean_OODB::numberof("stattest") != 3) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if (RedBean_OODB::maxof("stattest", "amount") != 3) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if (RedBean_OODB::minof("stattest", "amount") != 1) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if (RedBean_OODB::avgof("stattest", "amount") != 2) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if (RedBean_OODB::sumof("stattest", "amount") != 6) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if (count(RedBean_OODB::distinct("stattest", "amount")) != 3) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } RedBean_OODB::setLocking(true); $i = 3; SmartTest::instance()->testPack = "generate only valid classes?"; try { $i += RedBean_OODB::gen(""); SmartTest::instance()->progress(); } catch (Exception $e) { SmartTest::failedTest(); } //nothing try { $i += RedBean_OODB::gen("."); SmartTest::instance()->progress(); } catch (Exception $e) { SmartTest::failedTest(); } //illegal chars try { $i += RedBean_OODB::gen(","); SmartTest::instance()->progress(); } catch (Exception $e) { SmartTest::failedTest(); } //illegal chars try { $i += RedBean_OODB::gen("null"); SmartTest::instance()->progress(); } catch (Exception $e) { SmartTest::failedTest(); } //keywords try { $i += RedBean_OODB::gen("Exception"); SmartTest::instance()->progress(); } catch (Exception $e) { SmartTest::failedTest(); } //reserved SmartTest::instance()->progress(); SmartTest::instance()->testPack = "generate classes using Redbean?"; if (!class_exists("Bug")) { $i += RedBean_OODB::gen("Bug"); if ($i !== 4) { SmartTest::failedTest(); } } else { if ($i !== 3) { SmartTest::failedTest(); } } if (!class_exists("Bug")) { SmartTest::failedTest(); } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "use getters and setters"; $bug = new Bug(); $bug->setSomething(sha1("abc")); if ($bug->getSomething() != sha1("abc")) { SmartTest::failedTest(); } //can we use non existing props? --triggers fatal.. $bug->getHappy(); SmartTest::instance()->progress(); SmartTest::instance()->testPack = "Use boolean values and retrieve them with is()?"; if ($bug->isHappy()) { SmartTest::failedTest(); } SmartTest::instance()->progress(); $bug->setHappy(true); $bug->save(); $bug = new Bug(1); if (!$bug->isHappy()) { SmartTest::failedTest(); } SmartTest::instance()->progress(); $bug->setHappy(false); if ($bug->isHappy()) { SmartTest::failedTest(); } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "avoid race-conditions by locking?"; RedBean_OODB::gen("Cheese,Wine"); $cheese = new Cheese(); $cheese->setName('Brie'); $cheese->save(); $cheese = new Cheese(1); //try to mess with the locking system... $oldkey = RedBean_OODB::$pkey; RedBean_OODB::$pkey = 1234; $cheese = new Cheese(1); $cheese->setName("Camembert"); $ok = 0; try { $cheese->save(); } catch (ExceptionFailedAccessBean $e) { $ok = 1; } if (!$ok) { SmartTest::failedTest(); } SmartTest::instance()->progress(); $bordeaux = new Wine(); $bordeaux->setRegion("Bordeaux"); try { $bordeaux->add($cheese); } catch (ExceptionFailedAccessBean $e) { $ok = 1; } if (!$ok) { SmartTest::failedTest(); } SmartTest::instance()->progress(); try { $bordeaux->attach($cheese); } catch (ExceptionFailedAccessBean $e) { $ok = 1; } if (!$ok) { SmartTest::failedTest(); } SmartTest::instance()->progress(); try { $bordeaux->add(new Wine()); $ok = 1; } catch (ExceptionFailedAccessBean $e) { $ok = 0; } if (!$ok) { SmartTest::failedTest(); } SmartTest::instance()->progress(); RedBean_OODB::$pkey = $oldkey; $cheese = new Cheese(1); $cheese->setName("Camembert"); $ok = 0; try { $cheese->save(); $ok = 1; } catch (ExceptionFailedAccessBean $e) { $ok = 0; } if (!$ok) { SmartTest::failedTest(); } SmartTest::instance()->progress(); try { RedBean_OODB::$pkey = 999; RedBean_OODB::setLockingTime(0); $cheese = new Cheese(1); $cheese->setName("Cheddar"); $cheese->save(); RedBean_OODB::setLockingTime(10); SmartTest::instance()->progress(); } catch (Exception $e) { SmartTest::failedTest(); } try { RedBean_OODB::$pkey = 123; RedBean_OODB::setLockingTime(100); $cheese = new Cheese(1); $cheese->setName("Cheddar2"); $cheese->save(); RedBean_OODB::setLockingTime(10); SmartTest::failedTest(); } catch (Exception $e) { SmartTest::instance()->progress(); } try { RedBean_OODB::$pkey = 42; RedBean_OODB::setLockingTime(0); $cheese = new Cheese(1); $cheese->setName("Cheddar3"); $cheese->save(); RedBean_OODB::setLockingTime(10); SmartTest::instance()->progress(); } catch (Exception $e) { SmartTest::failedTest(); } //test value ranges SmartTest::instance()->progress(); SmartTest::instance()->testPack = "protect inner state of RedBean"; try { RedBean_OODB::setLockingTime(-1); SmartTest::failedTest(); } catch (ExceptionInvalidArgument $e) { } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "protect inner state of RedBean"; try { RedBean_OODB::setLockingTime(1.5); SmartTest::failedTest(); } catch (ExceptionInvalidArgument $e) { } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "protect inner state of RedBean"; try { RedBean_OODB::setLockingTime("aaa"); SmartTest::failedTest(); } catch (ExceptionInvalidArgument $e) { } SmartTest::instance()->progress(); SmartTest::instance()->testPack = "protect inner state of RedBean"; try { RedBean_OODB::setLockingTime(null); SmartTest::failedTest(); } catch (ExceptionInvalidArgument $e) { } SmartTest::instance()->progress(); //test convenient tree functions SmartTest::instance()->testPack = "convient tree functions"; if (!class_exists("Person")) { RedBean_OODB::gen("person"); } $donald = new Person(); $donald->setName("Donald"); $donald->save(); $kwik = new Person(); $kwik->setName("Kwik"); $kwik->save(); $kwek = new Person(); $kwek->setName("Kwek"); $kwek->save(); $kwak = new Person(); $kwak->setName("Kwak"); $kwak->save(); $donald->attach($kwik); $donald->attach($kwek); $donald->attach($kwak); if (count($donald->children()) != 3) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if (count($kwik->siblings()) != 2) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } //todo if ($kwik->hasParent($donald) != true) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($donald->hasParent($kwak) != false) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($donald->hasChild($kwak) != true) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($donald->hasChild($donald) != false) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($kwak->hasChild($kwik) != false) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($kwak->hasSibling($kwek) != true) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($kwak->hasSibling($kwak) != false) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($kwak->hasSibling($donald) != false) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } //copy SmartTest::instance()->testPack = "copy functions"; $kwak2 = $kwak->copy(); $id = $kwak2->save(); $kwak2 = new Person($id); if ($kwak->getName() != $kwak2->getName()) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } SmartTest::instance()->testPack = "countRelated"; R::gen("Blog,Comment"); $blog = new Blog(); $blog2 = new Blog(); $blog->setTitle("blog1"); $blog2->setTitle("blog2"); for ($i = 0; $i < 5; $i++) { $comment = new Comment(); $comment->setText("comment no. {$i} "); $blog->add($comment); } for ($i = 0; $i < 3; $i++) { $comment = new Comment(); $comment->setText("comment no. {$i} "); $blog2->add($comment); } if ($blog->numofComment() !== 5) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($blog2->numofComment() !== 3) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } SmartTest::instance()->testPack = "associate tables of the same name"; $blog = new Blog(); $blogb = new Blog(); $blog->title = 'blog a'; $blogb->title = 'blog b'; $blog->add($blogb); $b = $blog->getRelatedBlog(); if (count($b) !== 1) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } $b = array_pop($b); if ($b->title != 'blog b') { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } SmartTest::instance()->testPack = "inferTypeII patch"; $blog->rating = 4294967295.0; $blog->save(); $id = $blog->getID(); $blog2->rating = -1; $blog2->save(); $blog = new Blog($id); if ($blog->getRating() != 4294967295.0) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } SmartTest::instance()->testPack = "Longtext column type"; $blog->message = str_repeat("x", 65535); $blog->save(); $blog = new Blog($id); if (strlen($blog->message) != 65535) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } $rows = RedBean_OODB::$db->get("describe blog"); if ($rows[3]["Type"] != "text") { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } $blog->message = str_repeat("x", 65536); $blog->save(); $blog = new Blog($id); if (strlen($blog->message) != 65536) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } $rows = RedBean_OODB::$db->get("describe blog"); if ($rows[3]["Type"] != "longtext") { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } Redbean_OODB::clean(); SmartTest::instance()->testPack = "Export"; RedBean_OODB::gen("justabean"); $oBean = new JustABean(); $oBean->setA("a"); $oOtherBean = new OODBBean(); $oOtherBean->a = "b"; $oBean2 = new OODBBean(); $oBean->exportTo($oBean2); if ($oBean2->a !== "a") { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } $oBean2 = $oBean->exportTo($oBean2); if ($oBean2->a !== "a") { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } $oBean->exportTo($oBean2, $oOtherBean); if ($oBean2->a !== "b") { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } $arr = array(); $oBean->exportTo($arr); if ($arr["a"] !== "a") { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } $arr = array(); $arr = $oBean->exportTo($arr); if ($arr["a"] !== "a") { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } $arr = $oBean->exportTo($arr, $oOtherBean); if ($arr["a"] !== "b") { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } SmartTest::instance()->testPack = "Export Array"; $oBean->a = "a"; $oInnerBean = new JustABean(); $oInnerBean->setID(123); $oBean->innerbean = $oInnerBean; $arr = $oBean->exportAsArr(); if (!is_array($arr)) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($arr["innerbean"] !== 123) { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } if ($arr["a"] !== "a") { SmartTest::failedTest(); } else { SmartTest::instance()->progress(); } //test 1-to-n SmartTest::instance()->testPack = "1-to-n relations"; R::gen("Track,Disc"); $cd1 = new Disc(); $cd1->name = 'first'; $cd1->save(); $cd2 = new Disc(); $cd2->name = 'second'; $cd2->save(); $track = new Track(); $track->title = "song 1"; $track->belongsTo($cd1); $discs = $track->getRelatedDisc(); SmartTest::instance()->test(count($discs), 1); $track->belongsTo($cd2); $discs = $track->getRelatedDisc(); SmartTest::instance()->test(count($discs), 1); $track2 = new Track(); $track2->title = "song 2"; $cd1->exclusiveAdd($track2); SmartTest::instance()->test(count($track->getRelatedDisc()), 1); $cd2->exclusiveAdd($track2); SmartTest::instance()->test(count($track->getRelatedDisc()), 1); }