Commit 1ef5dfe5 by Scott

Handle basic logic for comment voting

parent 426ba54c
......@@ -657,10 +657,10 @@ function qa_get_vote_view($postorbasetype, $full = false, $enabledif = true)
// The 'level' and 'approve' permission errors are taken care of by disabling the voting buttons.
// Others are reported to the user after they click, in qa_vote_error_html(...)
if (is_array($postorbasetype)) { // deal with dual-use parameter
// deal with dual-use parameter
if (is_array($postorbasetype)) {
$basetype = $postorbasetype['basetype'];
$post = $postorbasetype;
} else {
$basetype = $postorbasetype;
$post = null;
......@@ -668,17 +668,34 @@ function qa_get_vote_view($postorbasetype, $full = false, $enabledif = true)
$disabledsuffix = '';
if ($basetype == 'Q' || $basetype == 'A') {
$view = $basetype == 'A' ? qa_opt('voting_on_as') : qa_opt('voting_on_qs');
switch($basetype)
{
case 'Q':
$view = qa_opt('voting_on_qs');
$permitOpt = 'permit_vote_q';
break;
case 'A':
$view = qa_opt('voting_on_as');
$permitOpt = 'permit_vote_a';
break;
case 'C':
$view = qa_opt('voting_on_cs');
$permitOpt = 'permit_vote_c';
break;
default:
$view = false;
break;
}
if (!($enabledif && ($basetype == 'A' || $full || !qa_opt('voting_on_q_page_only'))))
$disabledsuffix = '-disabled-page';
if (!$view) {
return false;
}
if (!$enabledif || ($basetype == 'Q' && !$full && qa_opt('voting_on_q_page_only'))) {
$disabledsuffix = '-disabled-page';
}
else {
if ($basetype == 'A')
$permiterror = isset($post) ? qa_user_post_permit_error('permit_vote_a', $post) : qa_user_permit_error('permit_vote_a');
else
$permiterror = isset($post) ? qa_user_post_permit_error('permit_vote_q', $post) : qa_user_permit_error('permit_vote_q');
$permiterror = isset($post) ? qa_user_post_permit_error($permitOpt, $post) : qa_user_permit_error($permitOpt);
if ($permiterror == 'level')
$disabledsuffix = '-disabled-level';
......@@ -694,10 +711,7 @@ function qa_get_vote_view($postorbasetype, $full = false, $enabledif = true)
}
}
} else
$view = false;
return $view ? ((qa_opt('votes_separated') ? 'updown' : 'net') . $disabledsuffix) : false;
return (qa_opt('votes_separated') ? 'updown' : 'net') . $disabledsuffix;
}
......
......@@ -52,11 +52,27 @@ function qa_vote_error_html($post, $vote, $userid, $topage)
return qa_lang_html('main/vote_disabled_queued');
}
$allowVoting = qa_opt($post['basetype'] == 'Q' ? 'voting_on_qs' : 'voting_on_as');
switch($post['basetype'])
{
case 'Q':
$allowVoting = qa_opt('voting_on_qs');
break;
case 'A':
$allowVoting = qa_opt('voting_on_as');
break;
case 'C':
$allowVoting = qa_opt('voting_on_cs');
break;
default:
$allowVoting = false;
break;
}
if (!$allowVoting || (isset($post['userid']) && isset($userid) && $post['userid'] == $userid)) {
// voting option should not have been presented (but could happen due to options change)
return qa_lang_html('main/vote_not_allowed');
}
if (($post['basetype'] === 'Q' || $post['basetype'] === 'A') && $allowVoting &&
(!isset($post['userid']) || !isset($userid) || $post['userid'] != $userid)
) {
$permiterror = qa_user_post_permit_error(($post['basetype'] == 'Q') ? 'permit_vote_q' : 'permit_vote_a', $post, QA_LIMIT_VOTES);
$errordownonly = !$permiterror && $vote < 0;
......@@ -85,10 +101,6 @@ function qa_vote_error_html($post, $vote, $userid, $topage)
return qa_lang_html('users/no_permission');
break;
}
} else {
// voting option should not have been presented (but could happen due to options change)
return qa_lang_html('main/vote_not_allowed');
}
}
......
......@@ -10,8 +10,9 @@ class AppOptionsTest extends PHPUnit_Framework_TestCase
'voting_on_cs' => 1,
'voting_on_q_page_only' => 1,
'votes_separated' => 0,
'permit_vote_a' => QA_PERMIT_USERS,
'permit_vote_q' => QA_PERMIT_USERS,
'permit_vote_a' => QA_PERMIT_USERS,
'permit_vote_c' => QA_PERMIT_USERS,
'permit_vote_down' => QA_PERMIT_USERS,
);
......@@ -61,14 +62,16 @@ class AppOptionsTest extends PHPUnit_Framework_TestCase
$this->assertSame('net-disabled-page', qa_get_vote_view('A', true, false));
$this->assertSame('net-disabled-page', qa_get_vote_view('A', false, false));
$this->assertSame(false, qa_get_vote_view('C', true));
$this->assertSame(false, qa_get_vote_view('C', false));
$this->assertSame('net', qa_get_vote_view('C', true));
$this->assertSame('net', qa_get_vote_view('C', false));
$qa_options_cache['voting_on_qs'] = 0;
$qa_options_cache['voting_on_as'] = 0;
$qa_options_cache['voting_on_cs'] = 0;
$this->assertSame(false, qa_get_vote_view('Q', true));
$this->assertSame(false, qa_get_vote_view('A', true));
$this->assertSame(false, qa_get_vote_view('C', true));
}
/**
......@@ -92,13 +95,15 @@ class AppOptionsTest extends PHPUnit_Framework_TestCase
$this->assertSame('updown-disabled-page', qa_get_vote_view('A', true, false));
$this->assertSame('updown-disabled-page', qa_get_vote_view('A', false, false));
$this->assertSame(false, qa_get_vote_view('C', true));
$this->assertSame(false, qa_get_vote_view('C', false));
$this->assertSame('updown', qa_get_vote_view('C', true));
$this->assertSame('updown', qa_get_vote_view('C', false));
$qa_options_cache['voting_on_qs'] = 0;
$qa_options_cache['voting_on_as'] = 0;
$qa_options_cache['voting_on_cs'] = 0;
$this->assertSame(false, qa_get_vote_view('Q', true));
$this->assertSame(false, qa_get_vote_view('A', true));
$this->assertSame(false, qa_get_vote_view('C', true));
}
......
<?php
require_once QA_INCLUDE_DIR.'app/votes.php';
require_once QA_INCLUDE_DIR.'app/options.php';
class AppVotesTest extends PHPUnit_Framework_TestCase
{
private $voteviewOpts = array(
'voting_on_qs' => 1,
'voting_on_as' => 1,
'voting_on_cs' => 1,
// 'voting_on_q_page_only' => 1,
// 'votes_separated' => 0,
'permit_vote_q' => QA_PERMIT_USERS,
'permit_vote_a' => QA_PERMIT_USERS,
'permit_vote_c' => QA_PERMIT_USERS,
'permit_vote_down' => QA_PERMIT_USERS,
);
private $mockQuestion = array(
'postid' => 16349,
'categoryid' => '',
'type' => 'Q',
'basetype' => 'Q',
'hidden' => 0,
'queued' => 0,
'acount' => 13,
'selchildid' => '',
'closedbyid' => '',
'upvotes' => 1,
'downvotes' => 0,
'netvotes' => 1,
'views' => 20,
'hotness' => 33319100000,
'flagcount' => 0,
'title' => 'To be or not to be?',
'tags' => 'question,answer',
'created' => 1344623702,
'name' => '',
'categoryname' => '',
'categorybackpath' => '',
'categoryids' => '',
'uservote' => 1,
'userflag' => 0,
'userfavoriteq' => '0',
'content' => 'That is the question.',
'notify' => '',
'updated' => 1409375832,
'updatetype' => 'E',
'format' => '',
'lastuserid' => 21981,
'lastip' => '',
'parentid' => '',
'lastviewip' => '',
'userid' => 1,
'cookieid' => '',
'createip' => '',
'points' => 140,
'flags' => 0,
'level' => 0,
'email' => '21981@example.com',
'handle' => 'QuestionAsker',
'avatarblobid' => '',
'avatarwidth' => '',
'avatarheight' => '',
'lasthandle' => 'QuestionAsker',
);
private $mockUser = array(
'userid' => 1,
'passsalt' => null,
'passcheck' => null,
'passhash' => 'passhash',
'email' => 'email',
'level' => 120,
'emailcode' => '',
'handle' => 'admin',
'created' => '',
'sessioncode' => '',
'sessionsource' => null,
'flags' => 265,
'loggedin' => '',
'loginip' => '',
'written' => '',
'writeip' => '',
'avatarblobid' => '',
'avatarwidth' => '',
'avatarheight' => '',
'points' => 100,
'wallposts' => 6,
);
/**
* Test voteview where upvotes/downvotes are combined
*/
public function test__qa_vote_error_html()
{
// set options/lang/user cache to bypass database
global $qa_options_cache, $qa_curr_ip_blocked, $qa_cached_logged_in_user, $qa_phrases_full;
$qa_options_cache = array_merge($qa_options_cache, $this->voteviewOpts);
$qa_curr_ip_blocked = false;
$qa_cached_logged_in_user = $this->mockUser;
$qa_phrases_full['main']['vote_not_allowed'] = 'Voting on this is not allowed';
$qa_phrases_full['main']['vote_disabled_hidden'] = 'You cannot vote on hidden posts';
$topage = '123/to-be-or-not-to-be';
$this->assertSame($qa_phrases_full['main']['vote_not_allowed'], qa_vote_error_html($this->mockQuestion, 1, 1, $topage));
$hiddenQ = $this->mockQuestion;
$hiddenQ['hidden'] = 1;
$this->assertSame($qa_phrases_full['main']['vote_disabled_hidden'], qa_vote_error_html($hiddenQ, 1, 17, $topage));
// can't test more right now due to qa_user_limits_remaining() call from qa_user_permit_error()
}
}
......@@ -2643,7 +2643,6 @@ input[type="submit"], button {
width: 12px;
height: 8px;
left: 0;
transform: scale(0.75);
}
.qa-c-list-item .qa-vote-first-button {
top: 0;
......@@ -2651,6 +2650,9 @@ input[type="submit"], button {
.qa-c-list-item .qa-vote-second-button {
top: 12px;
}
.qa-c-list-item .qa-vote-one-button {
top: 6px;
}
.qa-c-list-item .qa-vote-up-button {
background-position: -32px 0;
......
......@@ -495,7 +495,7 @@ class qa_html_theme extends qa_html_theme_base
*/
public function vote_count($post)
{
if ($post['raw']['basetype'] === 'C' && $post['netvotes_view']['data'] == 0) {
if ($post['raw']['basetype'] === 'C' && $post['raw']['netvotes'] == 0) {
$post['netvotes_view']['data'] = '';
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment