mention_helper_test.rb 3.61 KB
require_relative "../test_helper"

class MentionHelperTest < ActionView::TestCase

  include MentionHelper

  #helper ApplicationHelper

  def setup
    @person = create_user('user-test').person

    @friends = [
      create_user('friend-1').person, create_user('friend-2').person
    ]
    @person.add_friend @friends[0]
    @person.add_friend @friends[1]

    @community = fast_create(Community, :name=>'community-test')
    @members = [
      create_user('member-1').person, create_user('member-2').person
    ]
    @community.add_member @members[0]
    @community.add_member @members[1]

    @person.save!
    @community.add_member @person
    @community.save!

    @sample_message = "@#{@friends[0].identifier} and @#{@members[0].identifier} " +
                      "send an email to fulano@gmail.com " +
                      "and @#{@person.identifier}.And @#{@person.identifier} do nothing"

  end

  should 'mention regex match mentions at thet very start of a text' do
    assert_match mention_search_regex, '@mention-in-the-start and etc'
  end

  should 'mention regex match mentions throughout the text' do
    assert_match mention_search_regex, 'there is a mention in @middle of the text'
  end

  should 'mention regex not match email' do
    assert_no_match mention_search_regex, 'The email fulano@gmail.com is not a mention'
  end

  should 'verify if there is a mention on a given text' do
    assert_equal true, has_mentions?(@sample_message)
    assert_equal false, has_mentions?("fulano@gmail.com is not a mention")
  end

  should 'get the identifiers from the mentions on a text' do
    mentions = get_mentions @sample_message

    assert_equal 3, mentions.count
    assert_equal false, mentions.include?("gmail.com")
    assert_equal true, mentions.include?(@person.identifier)
    assert_equal true, mentions.include?(@friends[0].identifier)
    assert_equal true, mentions.include?(@members[0].identifier)
  end

  should 'get invalid mentions from a person' do
    invalid = get_invalid_mentions @sample_message, @person, @community
    assert_equal true, invalid.empty?

    @sample_message += " and @ciclano"
    invalid = get_invalid_mentions @sample_message, @person, @community
    assert_equal false, invalid.empty?
    assert_equal true, invalid.include?("ciclano")
  end

  should 'remove a given set of invalid mentions from a text' do
    invalid_mentions = [@friends[0].identifier, @members[0].identifier]
    text = remove_invalid_mentions @sample_message, invalid_mentions

    mentions = get_mentions text

    assert_equal 1, mentions.count
    assert_equal @person.identifier, mentions[0]
    assert_equal false, mentions.include?(@friends[0].identifier)
    assert_equal false, mentions.include?(@members[0].identifier)
  end

  should 'tell if there is valid mentions by removing the invalid ones' do
    article = Article.new :name => 'test article', :profile => @community
    article.save!

    comment = Comment.new :body => @sample_message, :author => @person, :source => article
    comment.save!

    assert_equal true, has_valid_mentions?(comment, :body, @person, @community)

    comment.body += " and @ciclano" # will have its mention removed
    assert_equal true, has_valid_mentions?(comment, :body, @person, @community) # only the "@" will be removed from ciclano
    assert_equal false, comment.body.include?("@ciclano")
    assert_equal true, comment.body.include?("ciclano")

    comment.body = "A total @invalid mentioned text"
    assert_equal false, has_valid_mentions?(comment, :body, @person, @community)
    assert_equal false, comment.body.include?("@invalid")
    assert_equal true, comment.body.include?("invalid")
  end
end