Skip to content

Commit

Permalink
Added tests (publiclab#2207)
Browse files Browse the repository at this point in the history
  • Loading branch information
SidharthBansal authored and jywarren committed Feb 5, 2018
1 parent 2b8508c commit c37001d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 59 deletions.
37 changes: 23 additions & 14 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,10 @@ def show
end

def show_for_author
# try for a matching /wiki/_TAGNAME_ or /_TAGNAME_
@wiki = Node.where(path: "/wiki/#{params[:id]}").try(:first) || Node.where(path: "/#{params[:id]}").try(:first)
@wiki = Node.find(@wiki.power_tag('redirect')) if @wiki&.has_power_tag('redirect')
if params[:id][-1..-1] == '*' # wildcard tags
@wildcard = true
@tags = Tag.where('name LIKE (?)', params[:id][0..-2] + '%')
else
@tags = Tag.where(name: params[:id])
end
@tagname = params[:id]

default_type = if params[:id].match('question:')
'questions'
else
Expand All @@ -156,21 +151,34 @@ def show_for_author
# params[:node_type] - this is an optional param
# if params[:node_type] is nil - use @default_type
@node_type = params[:node_type] || default_type
@user = User.find_by(name: params[:author])
@title = "'" + @tagname.to_s + "' by " + params[:author]

node_type = 'note' if @node_type == 'questions' || @node_type == 'note'
node_type = 'page' if @node_type == 'wiki'
node_type = 'map' if @node_type == 'maps'
qids = Node.questions.where(status: 1).collect(&:nid)

if params[:id][-1..-1] == '*' # wildcard tags
@wildcard = true
@tags = Tag.where('name LIKE (?)', params[:id][0..-2] + '%')
else
@tags = Tag.where(name: params[:id])
end
@tagname = params[:id]
@user = User.find_by(name: params[:author])

nodes = Tag.tagged_nodes_by_author(@tagname, @user)
.paginate(page: params[:page], per_page: 24)
.where(status: 1, type: node_type)
.paginate(page: params[:page], per_page: 24)

# breaks the parameter
# sets everything to an empty array
set_sidebar :tags, [params[:id]]

@notes = nodes.where('node.nid NOT IN (?)', qids) if @node_type == 'note'
@unpaginated = true
node_type = 'note' if @node_type == 'questions' || @node_type == 'note'
node_type = 'page' if @node_type == 'wiki'
node_type = 'map' if @node_type == 'maps'
@questions = nodes.where('node.nid IN (?)', qids) if @node_type == 'questions'
@wikis = nodes if @node_type == 'wiki'
@nodes = nodes if @node_type == 'maps'
@title = "'" + @tagname.to_s + "' by " + params[:author]
# the following could be refactored into a Tag.contributor_count method:
notes = Node.where(status: 1, type: 'note')
.select('node.nid, node.type, node.uid, node.status, term_data.*, community_tags.*')
Expand All @@ -196,6 +204,7 @@ def show_for_author
end
end


def widget
num = params[:n] || 4
nids = Tag.find_nodes_by_type(params[:id], 'note', num).collect(&:nid)
Expand Down
20 changes: 10 additions & 10 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,17 @@ def self.trending(limit = 5 , start_date = DateTime.now - 1.month , end_date = D
def self.tagged_nodes_by_author(tagname, user_id)
if tagname[-1..-1] == '*'
@wildcard = true
Node.where('term_data.name LIKE(?)', tagname[0..-2]+'%')
.includes(:node_tag, :tag)
.references(:term_data)
.order('node.nid DESC')
.where('node.uid = ?', user_id)
Node.includes(:node_tag, :tag)
.where('term_data.name LIKE(?) OR term_data.parent LIKE (?)', tagname[0..-2]+'%', tagname[0..-2]+'%')
.references(:term_data, :node_tag)
.where('node.uid = ?', user_id)
.order('node.nid DESC')
else
Node.where('term_data.name = ?', tagname)
.includes(:node_tag, :tag)
.order('node.nid DESC')
.references(:term_data)
.where('node.uid = ?', user_id)
Node.includes(:node_tag, :tag)
.where('term_data.name = ? OR term_data.parent = ?', tagname, tagname)
.references(:term_data, :node_tag)
.where('node.uid = ?', user_id)
.order('node.nid DESC')
end
end

Expand Down
3 changes: 2 additions & 1 deletion app/views/tag/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
<% unless @wildcard %><p style="clear:both;padding-top:8px;"><a style="margin-bottom:6px;" class="btn btn-primary" href="/post?tags=question:<%= params[:id].gsub('question:', '') %>&template=question&redirect=question"><i class="fa fa-white icon-question-circle"></i> <%= raw t('tag.show.ask_question', :tag => params[:id].gsub('question:', '')) %></a> <%= raw t('tag.show.or_subscribe_to_answer', :url1 => "/subscribe/tag/question:"+params[:id].gsub('question:', '')) %></p><% end %>

<ul class="nav nav-tabs" style="margin-top:10px;">
<% unless params[:id].match("question:") %><li<% if @node_type == "note" %> class="active"<% end %>><a href="/tag/<%= params[:id] %>"><i class="fa fa-file"></i> <%= raw t('tag.show.research_notes') %></a></li><% end %>
<% if params[:action] == "show" %>
<% unless params[:id].match("question:") %><li<% if @node_type == "note" %> class="active"<% end %>><a href="/tag/<%= params[:id] %>"><i class="fa fa-file"></i> <%= raw t('tag.show.research_notes') %></a></li><% end %>
<li<% if @node_type == "questions" %> class="active"<% end %>><a href="/questions/tag/<%= params[:id] %>"><i class="fa fa-question-circle"></i> <%= t('tag.show.questions') %></a></li>
<li<% if @node_type == "wiki" %> class="active"<% end %>><a href="/wiki/tag/<%= params[:id] %>"><i class="fa fa-book"></i> <%= raw t('tag.show.wiki_pages') %></a></li>
<li<% if @node_type == "maps" %> class="active"<% end %>><a href="/maps/tag/<%= params[:id] %>"><i class="fa fa-map-marker"></i> <%= t('tag.show.maps') %></a></li>
Expand All @@ -74,6 +74,7 @@
<li class="disabled"<% if @node_type == "contributors" %> class="active"<% end %>><a href="#" rel="tooltip" title="Contributors cannot be listed for wildcard tag searches" ><i class="fa fa-user"></i> <%= raw t('tag.show.contributors') %></a></li>
<% end %>
<% else %>
<% unless params[:id].match("question:") %><li<% if @node_type == "note" %> class="active"<% end %>><a href="/tag/<%= params[:id] %>/author/<%= params[:author] %>"><i class="fa fa-file"></i> <%= raw t('tag.show.research_notes') %></a></li><% end %>
<li<% if @node_type == "questions" %> class="active"<% end %>><a href="/questions/tag/<%= params[:id] %>/author/<%= params[:author] %>" ><i class="fa fa-question-circle"></i> <%= t('tag.show.questions') %></a></li>
<li<% if @node_type == "wiki" %> class="active"<% end %>><a href="/wiki/tag/<%= params[:id] %>/author/<%= params[:author] %>" ><i class="fa fa-book"></i> <%= raw t('tag.show.wiki_pages') %></a></li>
<li<% if @node_type == "maps" %> class="active"<% end %>><a href="/maps/tag/<%= params[:id] %>/author/<%= params[:author] %>" ><i class="fa fa-map-marker"></i> <%= t('tag.show.maps') %></a></li>
Expand Down
119 changes: 85 additions & 34 deletions test/functional/tag_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,30 +172,30 @@ def setup
end

test "wildcard tag show wiki pages with author" do
get :show_for_author, node_type: 'wiki', id: 'activity:*', author: 'jeff'
get :show_for_author, node_type: 'wiki', id: 'awes*', author: 'Bob'
assert :success
assert_not_nil :tags
assert :wildcard
assert assigns(:wildcard)
assert :wikis
assert assigns(:wikis).length > 0
assigns['wikis'].each do |node|
assert_equal 2, node.uid
assert node.has_tag('activity:*')
assert_equal 1, node.uid
assert node.has_tag('awes*')
end
assert_select '#note-graph', 0
assert_template 'tag/show'
end

test "tag show wiki pages with author" do
get :show_for_author, node_type: 'wiki', id: 'activity:spectrometer', author: 'jeff'
get :show, node_type: 'wiki', id: 'awesome', author: 'Bob'
assert :success
assert_not_nil :tags
assert :wildcard
assert_nil assigns(:wildcard)
assert :wikis
assert assigns(:wikis).length > 0
assigns['wikis'].each do |node|
assert_equal 2, node.uid
assert node.has_tag('activity:spectrometer')
assert_equal 1, node.uid
assert node.has_tag('awesome')
end
assert_template 'tag/show'
end
Expand All @@ -214,34 +214,34 @@ def setup
end

test 'show note with author and tagname without wildcard' do
get :show_for_author, id: 'test', author: 'jeff'
assert_response :success
assert_not_nil :tags
assert_not_nil :authors
assert_not_nil :notes
assert_nil assigns(:wildcard)
assert assigns['notes'].include?(nodes(:one))
assigns['notes'].each do |node|
assert_equal 2, node.uid
assert node.has_tag('test')
end
assert_template 'tag/show'
get :show_for_author, id: 'test', author: 'jeff'
assert_response :success
assert_not_nil :tags
assert_not_nil :authors
assert_not_nil :notes
assert_nil assigns(:wildcard)
assert assigns['notes'].include?(nodes(:one))
assigns['notes'].each do |node|
assert_equal 2, node.uid
assert node.has_tag('test')
end
assert_template 'tag/show'
end

test 'show note with author and tagname with wildcard' do
get :show_for_author, id: 'test*', author: 'jeff'
assert_response :success
assert_not_nil :tags
assert_not_nil :authors
assert_not_nil :notes
assert assigns(:wildcard)
assert assigns['notes'].include?(nodes(:one))
assert assigns['notes'].include?(nodes(:blog))
assigns['notes'].each do |node|
assert_equal 2, node.uid
assert node.has_tag('test*')
end
assert_template 'tag/show'
test 'show note with author and tagname with wildcard' do
get :show_for_author, id: 'test*', author: 'jeff'
assert_response :success
assert_not_nil :tags
assert_not_nil :authors
assert_not_nil :notes
assert assigns(:wildcard)
assert assigns['notes'].include?(nodes(:one))
assert assigns['notes'].include?(nodes(:blog))
assigns['notes'].each do |node|
assert_equal 2, node.uid
assert node.has_tag('test*')
end
assert_template 'tag/show'
end

test 'tag widget' do
Expand Down Expand Up @@ -447,10 +447,61 @@ def setup
assert_response :success
assert_select 'table' # ensure a table is shown
end

test 'rss with tagname and authorname' do
get :rss_for_tagged_with_author, tagname: 'test*', authorname: 'jeff', format: 'rss'
assert :success
assert_not_nil :notes
assert_equal 'application/rss+xml', @response.content_type
end

test 'should have active question tab for question for show_for_author' do
tag = tags(:question)
get :show_for_author, id: tag.name, author: 'jeff'
assert_select 'ul.nav-tabs' do
assert_select 'li.active' do
assert_select "a[href = '/questions/tag/question:spectrometer/author/jeff']", 1
end
end
assert_select '#questions.active', 1
end

test 'should take node type as note if tag is not a question tag for show_for_author' do
tag = tags(:awesome)

get :show_for_author, id: tag.name, author: 'jeff'

assert_equal 'note', assigns(:node_type)
end

test "does not show wiki for show_for_author" do
get :show_for_author, id: 'question', node_type: 'wiki', author: 'jeff'
assert_equal true, assigns(:wikis).empty?
end

test "wildcard does not show wiki for show_for_author" do
get :show_for_author, id: 'question:*', node_type: 'wiki', author: 'jeff'
assert_equal true, assigns(:wikis).empty?
end

test "does not show note for show_for_author" do
get :show_for_author, id: 'question', author: 'jeff'
assert_equal true, assigns(:notes).empty?
end

test "wildcard does not show note for show_for_author" do
get :show_for_author, id: 'question:*', author: 'jeff'
assert_equal true, assigns(:notes).empty?
end

test "wildcard does not show map for show_for_author" do
get :show_for_author, id: 'question:*', node_type: 'maps', author: 'jeff'
assert_equal true, assigns(:nodes).empty?
end

test " does not show map for show_for_author" do
get :show_for_author, id: 'question', node_type: 'maps', author: 'jeff'
assert_equal true, assigns(:nodes).empty?
end

end

0 comments on commit c37001d

Please sign in to comment.