Permalink
Browse files

Added more PHPUnit test cases, small security fix to search form

  • Loading branch information...
1 parent 90b4468 commit 339c1c8856f984e9ef4ba63e091b949331b493e1 @snytkine snytkine committed May 23, 2011
Showing with 2,296 additions and 753 deletions.
  1. +75 −29 lib/Lampcms/Answer.php
  2. +3 −4 lib/Lampcms/AnswerParser.php
  3. +1 −1 lib/Lampcms/CacheHeaders.php
  4. +2 −2 lib/Lampcms/Controllers/Answer.php
  5. +6 −7 lib/Lampcms/Controllers/Delete.php
  6. +1 −1 lib/Lampcms/Controllers/Editor.php
  7. +2 −1 lib/Lampcms/Controllers/Retag.php
  8. +5 −3 lib/Lampcms/Controllers/Search.php
  9. +14 −13 lib/Lampcms/Controllers/Shred.php
  10. +8 −4 lib/Lampcms/Controllers/Vote.php
  11. +1 −1 lib/Lampcms/Cookie.php
  12. +2 −4 lib/Lampcms/CookieAuth.php
  13. +24 −12 lib/Lampcms/DB.php
  14. +32 −15 lib/Lampcms/Interfaces/All.php
  15. +10 −2 lib/Lampcms/Modules/Observers/EmailNotifier.php
  16. +2 −9 lib/Lampcms/Modules/Search/IndexerMySQL.php
  17. +3 −4 lib/Lampcms/Modules/Search/MySQL.php
  18. +2 −3 lib/Lampcms/{ → Modules/Search}/TitleTagsTable.php
  19. +18 −4 lib/Lampcms/Mongo.php
  20. +3 −5 lib/Lampcms/MongoCache.php
  21. +12 −12 lib/Lampcms/MongoDoc.php
  22. +1 −2 lib/Lampcms/MongoIncrementor.php
  23. +18 −36 lib/Lampcms/Object.php
  24. +208 −70 lib/Lampcms/Question.php
  25. +1 −1 lib/Lampcms/RegBlock.php
  26. +0 −2 lib/Lampcms/Relatedtags.php
  27. +15 −15 lib/Lampcms/Request.php
  28. +2 −2 lib/Lampcms/Responder.php
  29. +0 −1 lib/Lampcms/SearchFactory.php
  30. +0 −218 lib/Lampcms/SimilarItems.php
  31. +1 −1 lib/Lampcms/String.php
  32. +0 −1 lib/Lampcms/Stub.php
  33. +2 −2 lib/Lampcms/Tokenizer.php
  34. +7 −35 lib/Lampcms/User.php
  35. +9 −0 lib/Lampcms/UserAuth.php
  36. +1 −1 lib/Lampcms/UserTags.php
  37. +1 −2 lib/Lampcms/UserTwitter.php
  38. +151 −40 lib/Lampcms/Utf8String.php
  39. +11 −97 lib/Lampcms/Validate.php
  40. +1 −3 lib/Lampcms/WebPage.php
  41. +16 −1 phpunit.xml
  42. +286 −0 tests/AnswerTest.php
  43. +0 −12 tests/ArrayDefaultsTest.php
  44. +75 −0 tests/Fixtures/MockAnswer.php
  45. +3 −3 tests/Fixtures/MockQuestion.php
  46. +2 −3 tests/Fixtures/MockUser.php
  47. +72 −0 tests/Fixtures/tplQtags.php
  48. +2 −2 tests/LampcmsArrayTest.php
  49. +85 −0 tests/LampcmsObjectTest.php
  50. +37 −21 tests/LampcmsUnitTestCase.php
  51. +102 −0 tests/MongoIncrementorTest.php
  52. +312 −11 tests/QuestionTest.php
  53. +0 −1 tests/RegistryTest.php
  54. +201 −0 tests/RequestTest.php
  55. +89 −0 tests/ResourceTest.php
  56. +1 −1 tests/UserTest.php
  57. +173 −0 tests/Utf8StringTest.php
  58. +72 −8 tests/bootstrap.php
  59. +8 −7 www/index.php
  60. +11 −8 www/style/1/www/tplQrecent.php
  61. +9 −9 www/style/1/www/tplSearchForm.php
  62. +5 −1 www/style/1/www/tplSimquestions.php
  63. +80 −0 www/update_comments.php
View
@@ -86,8 +86,7 @@ public function getResourceTypeId(){
* @return object $this
*/
public function setAccepted(){
- $this->offsetSet('accepted', true);
- $this->touch();
+ parent::offsetSet('accepted', true);
return $this;
}
@@ -99,8 +98,7 @@ public function setAccepted(){
* @return object $this
*/
public function unsetAccepted(){
- $this->offsetSet('accepted', false);
- $this->touch();
+ parent::offsetSet('accepted', false);
return $this;
}
@@ -150,8 +148,8 @@ public function getUsername(){
*/
public function setDeleted(User $user, $reason = null){
if(0 === $this->getDeletedTime()){
- $this->offsetSet('i_del_ts', time());
- $this->offsetSet('a_deleted',
+ parent::offsetSet('i_del_ts', time());
+ parent::offsetSet('a_deleted',
array(
'username' => $user->getDisplayName(),
'i_uid' => $user->getUid(),
@@ -160,8 +158,6 @@ public function setDeleted(User $user, $reason = null){
'hts' => date('F j, Y g:i a T')
)
);
-
- $this->touch();
}
return $this;
@@ -191,9 +187,7 @@ public function setEdited(User $user, $reason = ''){
'reason' => $reason,
'hts' => date('F j, Y g:i a T'));
- $this->offsetSet('a_edited', $aEdited);
-
- $this->touch();
+ parent::offsetSet('a_edited', $aEdited);
return $this;
}
@@ -246,14 +240,14 @@ public function addUpVote($inc = 1){
$score = (int)$this->offsetGet('i_votes');
$total = ($score + $inc);
- $this->offsetSet('i_up', max(0, ($tmp + $inc)) );
- $this->offsetSet('i_votes', $total );
+ parent::offsetSet('i_up', max(0, ($tmp + $inc)) );
+ parent::offsetSet('i_votes', $total );
/**
* Plural extension handling
*/
$v_s = (1 === abs($total) ) ? '' : 's';
- $this->offsetSet('v_s', $v_s);
+ parent::offsetSet('v_s', $v_s);
return $this;
}
@@ -273,14 +267,14 @@ public function addDownVote($inc = 1){
$score = (int)$this->offsetGet('i_votes');
$total = ($score - $inc);
- $this->offsetSet('i_down', max(0, ($tmp + $inc)) );
- $this->offsetSet('i_votes', $total);
+ parent::offsetSet('i_down', max(0, ($tmp + $inc)) );
+ parent::offsetSet('i_votes', $total);
/**
* Plural extension handling
*/
$v_s = (1 === abs($total) ) ? '' : 's';
- $this->offsetSet('v_s', $v_s);
+ parent::offsetSet('v_s', $v_s);
return $this;
}
@@ -387,11 +381,11 @@ public function addComment(CommentParser $oComment){
* because we don't need them here
*/
$aComment = $oComment->getArrayCopy();
- $aComment = array_intersect_key($aComment, array_flip($aKeys));
+ $aComment = \array_intersect_key($aComment, array_flip($aKeys));
$aComments[] = $aComment;
- $this->offsetSet('comments', $aComments);
+ $this->offsetSet('a_comments', $aComments);
$this->increaseCommentsCount();
return $this;
@@ -412,16 +406,29 @@ public function getCommentsCount(){
/**
*
- * Enter description here ...
+ * Increase value of i_comments by 1
+ * The i_comments is a counter
+ *
+ * @return object $this
*/
- public function increaseCommentsCount(){
+ public function increaseCommentsCount($count = 1){
+ if(!is_int($count)){
+ throw new \InvalidArgumentException('$count must be integer. was: '.gettype($count));
+ }
+
/**
* Now increase comments count
*/
$commentsCount = $this->getCommentsCount();
d('$commentsCount '.$commentsCount);
- $this->offsetSet('i_comments', ($commentsCount + 1) );
+ /**
+ * Must use parent::offsetSet because
+ * $this->offsetSet will point back to this
+ * method and enter infinite loop untill
+ * we run out of memory
+ */
+ parent::offsetSet('i_comments', ($commentsCount + $count) );
return $this;
}
@@ -439,30 +446,30 @@ public function increaseCommentsCount(){
*/
public function deleteComment($id){
- if(!$this->checkOffset('comments')){
+ if(0 === $this->getCommentsCount()){
e('This question does not have any comments');
return $this;
}
- $aComments = $this->offsetGet('comments');
+ $aComments = $this->offsetGet('a_comments');
for($i = 0; $i<count($aComments); $i+=1){
if($id == $aComments[$i]['_id']){
d('unsetting comment: '.$i);
- array_splice($aComments, $i, 1);
+ \array_splice($aComments, $i, 1);
break;
}
}
$newCount = count($aComments);
if( 0 === $newCount){
- $this->offsetUnset('comments');
+ $this->offsetUnset('a_comments');
} else {
- $this->offsetSet('comments', $aComments);
+ $this->offsetSet('a_comments', $aComments);
}
- $this->offsetSet('i_comments', $newCount );
+ $this->increaseCommentsCount(-1);
return $this;
}
@@ -475,7 +482,8 @@ public function deleteComment($id){
*
*/
public function getComments(){
- return $this->getFallback('comments', array());
+
+ return $this->offsetGet('a_comments');
}
@@ -502,4 +510,42 @@ public function getQuestionOwnerId(){
return (int)$this->offsetGet('i_quid');
}
+
+ /**
+ * This method prevents setting some
+ * values directly
+ *
+ * (non-PHPdoc)
+ * @see ArrayObject::offsetSet()
+ */
+ public function offsetSet($index, $newval){
+ switch($index){
+ case 'accepted':
+ throw new DevException('value of accepted cannot be set directly. Use setAccepted() or unsetAccepted() methods');
+ break;
+
+ case 'i_comments':
+ throw new DevException('value of i_comments cannot be set directly. Use increaseCommentsCount() method');
+ break;
+
+ case 'i_down':
+ case 'i_up':
+ case 'i_votes':
+ throw new DevException('value of '.$index.' keys cannot be set directly. Use addDownVote or addUpVote to add votes');
+ break;
+
+ case 'a_deleted':
+ case 'i_del_ts':
+ throw new DevException('value of '.$index.' cannot be set directly. Must use setDeleted() method for that');
+ break;
+
+ case 'a_edited':
+ throw new DevException('value of a_edited cannot be set directly. Must use setEdited() method for that');
+ break;
+
+ default:
+ parent::offsetSet($index, $newval);
+ }
+ }
+
}
@@ -193,7 +193,7 @@ protected function makeAnswer(){
$uid = $this->oSubmittedAnswer->getUserObject()->getUid();
$qid = $this->oSubmittedAnswer->getQid();
- $hash = hash('md5', strtolower($htmlBody.$qid));
+ $hash = hash('md5', \mb_strtolower($htmlBody.$qid));
/**
*
@@ -364,7 +364,8 @@ protected function updateQuestion(){
$this->oQuestion->updateAnswerCount()
->addContributor($oUser)
- ->setLastAnswerer($oUser);
+ ->setLatestAnswer($oUser, $this->oAnswer)
+ ->touch();
return $this;
}
@@ -377,8 +378,6 @@ protected function updateQuestion(){
* @return object $this
*/
protected function followQuestion(){
- d('cp');
-
$oFollowManager = new FollowManager($this->oRegistry);
$oFollowManager->followQuestion($this->oRegistry->Viewer, $this->oQuestion);
@@ -122,7 +122,7 @@ public static function processCacheHeaders($etag = null, $lastModified = null, $
* may be notified
*
*/
- if(headers_sent($file, $line)){
+ if(\headers_sent($file, $line)){
e('LampcmsError Headers have already been sent in file '.$file. ' on line '.$line);
return true;
@@ -112,8 +112,8 @@ protected function process(){
$oAdapter = new AnswerParser($this->oRegistry);
try{
$oAnswer = $oAdapter->parse(new SubmittedAnswerWWW($this->oRegistry, $formVals));
- d('cp created new question');
- d('ans id: '.$oAnswer->_id);
+ d('cp created new question: '.print_r($oAnswer->getArrayCopy(), 1));
+ d('ans id: '.$oAnswer->getResourceId());
/**
* In case of ajax we need to send out a
@@ -215,17 +215,15 @@ protected function updateQuestion(){
$oQuestion = new \Lampcms\Question($this->oRegistry);
$oQuestion->by_id($this->oResource['i_qid']);
- $oQuestion->updateAnswerCount(-1);
+ $oQuestion->removeAnswer($this->oResource);
if((true === $this->oResource['accepted'])){
d('this was an accepted answer');
- $this->oResource['accepted'] = false;
- $oQuestion->offsetUnset('i_sel_ans');
+ $this->oResource->unsetAccepted();
}
- $oQuestion->removeContributor($this->oResource['i_uid'])
- ->touch()->save();
+ $oQuestion->touch()->save();
}
return $this;
@@ -313,7 +311,8 @@ protected function setDeleted(){
*/
$this->updateTags();
$this->removeFromIndex();
- $this->oResource->setDeleted($this->oRegistry->Viewer, $this->oRequest['note']);
+ $this->oResource->setDeleted($this->oRegistry->Viewer, $this->oRequest['note'])
+ ->touch();
d('new resource data: '.print_r($this->oResource->getArrayCopy(), 1));
@@ -360,7 +359,7 @@ protected function updateTags(){
}
} else {
$oQuestion = new \Lampcms\Question($this->oRegistry);
- $oQuestion->by_id($this->oResource['i_qid']);
+ $oQuestion->by_id($this->oResource->getQuestionId());
d('tags: ' . print_r($oQuestion['a_tags'], 1));
}
@@ -148,7 +148,7 @@ protected function process(){
}
$this->oResource->setEdited($this->oRegistry->Viewer, \strip_tags($formVals['reason']));
- $this->oResource->save();
+ $this->oResource->touch()->save();
$this->oRegistry->Dispatcher->post($this->oResource, 'onEdit');
@@ -292,7 +292,8 @@ protected function addNewTags(){
*/
protected function updateQuestion(){
- $this->oQuestion->retag($this->oRegistry->Viewer, $this->aSubmitted)->save();
+ $this->oQuestion->retag($this->oRegistry->Viewer, $this->aSubmitted)
+ ->save();
return $this;
}
@@ -75,6 +75,8 @@ class Search extends WebPage
* @var bool
*/
protected $notAjaxPaginatable = true;
+
+ //protected $bRequirePost = true;
/**
* (non-PHPdoc)
@@ -87,23 +89,23 @@ protected function main(){
* $_GET as underlying array, and php
* already decodes $_GET or $_POST vars
*/
- $this->term = $this->oRequest['q'];
+ $this->term = $this->oRegistry->Request->getUTF8('q')->stripTags();
$this->aPageVars['qheader'] = '<h1>Search results for: '.$this->term.'</h1>';
$this->aPageVars['title'] = 'Questions matching &#39;'.$this->term.'&#39;';
d('$this->term: '.$this->term);
$this->oSearch = SearchFactory::factory($this->oRegistry);
- $this->oSearch->search();
+ $this->oSearch->search($this->term);
$this->makeTopTabs()
->makeInfo()
->makeBody();
}
protected function makeTopTabs(){
- d('cp');
+
$tabs = Urhere::factory($this->oRegistry)->get('tplToptabs', 'questions');
$this->aPageVars['topTabs'] = $tabs;
Oops, something went wrong.

0 comments on commit 339c1c8

Please sign in to comment.