Skip to content

Commit

Permalink
Tagging tests jasmine (#917)
Browse files Browse the repository at this point in the history
* tagging functional tests

* has_tag and add_tag fixes and tests

* tag id test fix

* re-activate jasmine tagging test test
  • Loading branch information
jywarren authored Oct 24, 2016
1 parent c4723fa commit 91f5864
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 35 deletions.
21 changes: 12 additions & 9 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def barnstar
def create
params[:name] ||= ""
tagnames = params[:name].split(',')
@output = { :errors => [],
:saved => [],
@output = {
errors: [],
saved: []
}
@tags = [] # not used except in tests for now

Expand All @@ -105,7 +106,7 @@ def create

# this should all be done in the model:

if DrupalTag.exists?(tagname,params[:nid])
if DrupalTag.exists?(tagname, params[:nid])
@output[:errors] << I18n.t('tag_controller.tag_already_exists')
else
# "with:foo" coauthorship powertag: by author only
Expand All @@ -119,12 +120,12 @@ def create
elsif tagname[0..4] == "rsvp:" && current_user.username != tagname.split(':')[1]
@output[:errors] << I18n.t('tag_controller.only_RSVP_for_yourself')
else
saved,tag = node.add_tag(tagname.strip,current_user)
saved, tag = node.add_tag(tagname.strip, current_user)
if saved
@tags << tag
@output[:saved] << [tag.name,tag.id]
@output[:saved] << [tag.name, tag.id]
else
@output[:errors] << I18n.t('tag_controller.error_tags')+tag.errors[:name].first
@output[:errors] << I18n.t('tag_controller.error_tags') + tag.errors[:name].first
end
end
end
Expand Down Expand Up @@ -166,15 +167,15 @@ def delete

def suggested
if params[:id].length > 2
suggestions = []
@suggestions = []
# filtering out tag spam by requiring tags attached to a published node
DrupalTag.where('name LIKE ?', "%" + params[:id] + "%")
.includes(:drupal_node)
.where('node.status = 1')
.limit(10).each do |tag|
suggestions << tag.name.downcase
@suggestions << tag.name.downcase
end
render :json => suggestions.uniq
render :json => @suggestions.uniq
else
render :json => []
end
Expand Down Expand Up @@ -234,6 +235,8 @@ def contributors_index
render :template => "tag/contributors-index"
end

# let's not use session for tags storage; deprecate these unless they're being used
# to remember and persist recently searched for tags:
def add_tag
unless session[:tags]
session[:tags] = {}
Expand Down
10 changes: 5 additions & 5 deletions app/models/drupal_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -700,13 +700,13 @@ def add_barnstar(tagname,giver)

def add_tag(tagname,user)
tagname = tagname.downcase
unless self.has_tag(tagname)
unless self.has_tag_without_aliasing(tagname)
saved = false
tag = DrupalTag.find_by_name(tagname) || DrupalTag.new({
:vid => 3, # vocabulary id; 1
:name => tagname,
:description => "",
:weight => 0
vid: 3, # vocabulary id; 1
name: tagname,
description: "",
weight: 0
})
ActiveRecord::Base.transaction do
if tag.valid?
Expand Down
4 changes: 2 additions & 2 deletions app/models/drupal_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DrupalUsers < ActiveRecord::Base
has_many :drupal_comments, :foreign_key => 'uid'
has_one :location_tag, :foreign_key => 'uid', :dependent => :destroy

searchable do
searchable :if => proc { |user| user.status == 1 } do
string :name
string :mail
string :status
Expand Down Expand Up @@ -215,4 +215,4 @@ def increase_likes_unbanned
node.drupal_node.save!
end
end
end
end
4 changes: 2 additions & 2 deletions app/views/tag/_tagging.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<p>
<span id="tags">
<% @node.community_tags.each do |tag| %>
<span id="tag_<%= tag.id %>" class="label label-primary" data-toggle="tooltip" data-html="true" title="created by <strong><%= tag.try(:author).try(:username) %></strong> <%= time_ago_in_words(Time.at(tag.date)) %> ago">
<a href="<%= "/maps" if @node.type == "map" %>/tag/<%= tag.name %>"><%= tag.name %></a>
<span id="tag_<%= tag.tid %>" class="label label-primary" data-toggle="tooltip" data-html="true" title="created by <strong><%= tag.try(:author).try(:username) %></strong> <%= time_ago_in_words(Time.at(tag.date)) %> ago">
<a class="tag-name" href="<%= "/maps" if @node.type == "map" %>/tag/<%= tag.name %>"><%= tag.name %></a>
<% if current_user && (tag.uid == @node.uid || current_user.role == "admin" || current_user.role == "moderator") %>
<a class="tag-delete" data-remote="true" href="/tag/delete/<%= @node.id %>/<%= tag.tid %>" data-tag-id="<%= tag.tid %>">x</a>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion spec/javascripts/tagging_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("Tagging", function() {


// this should test addTag(tagname, selector) from tagging.js, which is an ajax request
xit("tests tag deletion", function() {
it("tests tag deletion", function() {

$('tags.label:first a.tag-delete').trigger('click');

Expand Down
57 changes: 41 additions & 16 deletions test/functional/tag_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# def barnstar
# def create
# def delete
# def suggested
# def contributors_index

require 'test_helper'
Expand All @@ -13,12 +10,24 @@ def setup
end

# create accepts comma-delimited list of tags
test "add tag" do
test "add one or two tags" do
UserSession.create(rusers(:bob))

post :create, :name => 'mytag', :nid => node(:one).nid, :uid => rusers(:bob).id
post :create, name: 'mytag', nid: node(:one).nid, uid: rusers(:bob).id

assert_equal 'mytag', assigns[:tags].last.name
assert_redirected_to(node(:one).path)

post :create, name: 'mysecondtag,mythirdtag', nid: node(:one).nid, uid: rusers(:bob).id

assert_equal 'mysecondtag', assigns[:tags][assigns[:tags].length - 2].name
assert_equal 'mythirdtag', assigns[:tags].last.name
assert_redirected_to(node(:one).path)

xhr :post, :create, name: 'myfourthtag,myfifthtag', nid: node(:one).nid, uid: rusers(:bob).id

assert_response :success
assert_equal [["myfourthtag", DrupalTag.find_by_name("myfourthtag").tid], ["myfifthtag", DrupalTag.find_by_name("myfifthtag").tid]], JSON.parse(response.body)['saved']
end

test "validate unused tag" do
Expand Down Expand Up @@ -204,6 +213,15 @@ def setup
assert_select '#questions.active', 1
end

test "can create tag instance (community_tag) using a parent tag" do
UserSession.create(rusers(:bob))

post :create, name: 'spectrometry', nid: node(:one).nid, uid: rusers(:bob).id

assert_equal 'spectrometry', assigns[:tags].last.name
assert_redirected_to(node(:one).path)
end

test "shows things tagged with child tag" do
tag = tags(:spectrometer)
tag.parent = "spectrometry"
Expand Down Expand Up @@ -241,20 +259,27 @@ def setup
assert_false assigns(:notes).first.has_tag_without_aliasing('spectrometry')
assert assigns(:notes).first.has_tag_without_aliasing('spectrometer')
end

test "shows suggested tags" do
get :suggested, id: 'spectr'

assert_equal 3, assigns(:suggestions).length
assert_equal ["question:spectrometer","spectrometer","activity:spectrometer"], JSON.parse(response.body)
end

test "should choose I18n for tag controller" do
available_testing_locales.each do |lang|
old_controller = @controller
@controller = SettingsController.new
get :change_locale, :locale => lang.to_s
@controller = old_controller
UserSession.create(rusers(:bob))
post :create, :name => 'mytag', :nid => node(:one).nid, :uid => rusers(:bob)
post :create, :name => 'mytag', :nid => node(:one).nid, :uid => rusers(:bob)
assert_equal I18n.t('tag_controller.tag_already_exists'), assigns['output']['errors'][0]
old_controller = @controller
@controller = SettingsController.new

get :change_locale, :locale => lang.to_s

@controller = old_controller

UserSession.create(rusers(:bob))
post :create, :name => 'mytag', :nid => node(:one).nid, :uid => rusers(:bob)
post :create, :name => 'mytag', :nid => node(:one).nid, :uid => rusers(:bob)
assert_equal I18n.t('tag_controller.tag_already_exists'), assigns['output']['errors'][0]
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/unit/drupal_node_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class DrupalNodeTagTest < ActiveSupport::TestCase
assert_false DrupalTag.find_nodes_by_type_with_all_tags(['spectrometry']).to_a.include?(node)
assert DrupalTag.find_nodes_by_type('spectrometer').to_a.include?(node)
assert DrupalTag.find_nodes_by_type_with_all_tags(['spectrometer']).to_a.include?(node)

# test node.add_tag, which uses has_tag
saved, tag = node.add_tag('spectrometry', rusers(:bob))
assert saved
assert_not_nil tag
end

test "aliasing of tags which have parent matching initial tag" do
Expand Down

0 comments on commit 91f5864

Please sign in to comment.