Compare View

switch
from
...
to
 
Commits (2)
app/helpers/mention_helper.rb
1 1 module MentionHelper
2 2 def mention_search_regex
3   - /(?:^\s?|\s)@([^\s]+)/m
  3 + /(?:^\s?|\s)@([^\s?:^\.]+)/m
4 4 end
5 5  
6 6 def has_mentions? text=""
... ...
test/unit/mention_helper_test.rb 0 → 100644
... ... @@ -0,0 +1,103 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class MentionHelperTest < ActionView::TestCase
  4 +
  5 + include MentionHelper
  6 +
  7 + #helper ApplicationHelper
  8 +
  9 + def setup
  10 + @person = create_user('user-test').person
  11 +
  12 + @friends = [
  13 + create_user('friend-1').person, create_user('friend-2').person
  14 + ]
  15 + @person.add_friend @friends[0]
  16 + @person.add_friend @friends[1]
  17 +
  18 + @community = fast_create(Community, :name=>'community-test')
  19 + @members = [
  20 + create_user('member-1').person, create_user('member-2').person
  21 + ]
  22 + @community.add_member @members[0]
  23 + @community.add_member @members[1]
  24 +
  25 + @person.save!
  26 + @community.add_member @person
  27 + @community.save!
  28 +
  29 + @sample_message = "@#{@friends[0].identifier} and @#{@members[0].identifier} " +
  30 + "send an email to fulano@gmail.com " +
  31 + "and @#{@person.identifier}.And @#{@person.identifier} do nothing"
  32 +
  33 + end
  34 +
  35 + should 'mention regex match mentions at thet very start of a text' do
  36 + assert_match mention_search_regex, '@mention-in-the-start and etc'
  37 + end
  38 +
  39 + should 'mention regex match mentions throughout the text' do
  40 + assert_match mention_search_regex, 'there is a mention in @middle of the text'
  41 + end
  42 +
  43 + should 'mention regex not match email' do
  44 + assert_no_match mention_search_regex, 'The email fulano@gmail.com is not a mention'
  45 + end
  46 +
  47 + should 'verify if there is a mention on a given text' do
  48 + assert_equal true, has_mentions?(@sample_message)
  49 + assert_equal false, has_mentions?("fulano@gmail.com is not a mention")
  50 + end
  51 +
  52 + should 'get the identifiers from the mentions on a text' do
  53 + mentions = get_mentions @sample_message
  54 +
  55 + assert_equal 3, mentions.count
  56 + assert_equal false, mentions.include?("gmail.com")
  57 + assert_equal true, mentions.include?(@person.identifier)
  58 + assert_equal true, mentions.include?(@friends[0].identifier)
  59 + assert_equal true, mentions.include?(@members[0].identifier)
  60 + end
  61 +
  62 + should 'get invalid mentions from a person' do
  63 + invalid = get_invalid_mentions @sample_message, @person, @community
  64 + assert_equal true, invalid.empty?
  65 +
  66 + @sample_message += " and @ciclano"
  67 + invalid = get_invalid_mentions @sample_message, @person, @community
  68 + assert_equal false, invalid.empty?
  69 + assert_equal true, invalid.include?("ciclano")
  70 + end
  71 +
  72 + should 'remove a given set of invalid mentions from a text' do
  73 + invalid_mentions = [@friends[0].identifier, @members[0].identifier]
  74 + text = remove_invalid_mentions @sample_message, invalid_mentions
  75 +
  76 + mentions = get_mentions text
  77 +
  78 + assert_equal 1, mentions.count
  79 + assert_equal @person.identifier, mentions[0]
  80 + assert_equal false, mentions.include?(@friends[0].identifier)
  81 + assert_equal false, mentions.include?(@members[0].identifier)
  82 + end
  83 +
  84 + should 'tell if there is valid mentions by removing the invalid ones' do
  85 + article = Article.new :name => 'test article', :profile => @community
  86 + article.save!
  87 +
  88 + comment = Comment.new :body => @sample_message, :author => @person, :source => article
  89 + comment.save!
  90 +
  91 + assert_equal true, has_valid_mentions?(comment, :body, @person, @community)
  92 +
  93 + comment.body += " and @ciclano" # will have its mention removed
  94 + assert_equal true, has_valid_mentions?(comment, :body, @person, @community) # only the "@" will be removed from ciclano
  95 + assert_equal false, comment.body.include?("@ciclano")
  96 + assert_equal true, comment.body.include?("ciclano")
  97 +
  98 + comment.body = "A total @invalid mentioned text"
  99 + assert_equal false, has_valid_mentions?(comment, :body, @person, @community)
  100 + assert_equal false, comment.body.include?("@invalid")
  101 + assert_equal true, comment.body.include?("invalid")
  102 + end
  103 +end
... ...