cirandas.net

ref: master

test/functional/profile_search_controller_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require_relative '../test_helper'

class ProfileSearchControllerTest < ActionController::TestCase
  def setup
    @controller = ProfileSearchController.new

    @person = fast_create(Person)
  end
  attr_reader :person

  should 'espape xss attack' do
    @controller.expects(:profile).returns(person).at_least_once
    get 'index', :profile => person.identifier, :q => '<wslite>'
    assert_no_tag :tag => 'wslite'
  end

  should 'render success in search' do
    get :index, :profile => person.identifier, :q => 'something not important'
    assert_response :success
  end

  should 'search for articles' do
    article = TextArticle.create(:name => 'My article', :body => 'Article to test profile search', :profile => person)

    get 'index', :profile => person.identifier, :q => 'article to test'
    assert_includes assigns(:results), article
  end

  should 'not display articles from another profile' do
    article = TextArticle.create(:name => 'My article', :body => 'Article to test profile search', :profile => person)
    article2 = TextArticle.create(:name => 'Another article', :body => 'Article from someone else', :profile => fast_create(Person))

    get 'index', :profile => person.identifier, :q => 'article'
    assert_includes assigns(:results), article
    assert_not_includes assigns(:results), article2
  end

  should 'display search results' do
    article1 = fast_create(Article, {:body => '<p>Article to test profile search</p>', :profile_id => person.id}, :search => true)
    article2 = fast_create(Article, {:body => '<p>Another article to test profile search</p>', :profile_id => person.id}, :search => true)

    get 'index', :profile => person.identifier, :q => 'article'

    [article1, article2].each do |article|
      assert_tag :tag => 'li', :descendant => { :tag => 'a', :content => article.short_lead, :attributes => { :class => /article-details/ }}
    end
  end

  should 'paginate results listing' do
    (1..11).each do |i|
      TextArticle.create!(:name => "Article #{i}", :profile => person, :language => 'en')
    end

    get 'index', :profile => person.identifier, :q => 'Article'

    assert_equal 10, assigns('results').size
    assert_tag :tag => 'a', :attributes => { :href => "/profile/#{person.identifier}/search?page=2&amp;q=Article", :rel => 'next' }
  end

  should 'display abstract if given' do
    article1 = TextArticle.create(:name => 'Article 1', :abstract => 'Abstract to test', :body => '<p>Article to test profile search</p>', :profile => person)
    article2 = TextArticle.create(:name => 'Article 2', :body => '<p>Another article to test profile search</p>', :profile => person)

    get 'index', :profile => person.identifier, :q => 'article to test'

    assert_tag :tag => 'li', :descendant => { :tag => 'a', :content => article1.abstract, :attributes => { :class => /article-details/ }}
    assert_no_tag :tag => 'li', :descendant => { :tag => 'a', :content => 'Article to test profile search', :attributes => { :class => /article-details/ }}

    assert_tag :tag => 'li', :descendant => { :tag => 'a', :content => 'Another article to test profile search', :attributes => { :class => /article-details/ }}
  end

  should 'display nothing if search is blank' do
    article1 = TextArticle.create(:name => 'Article 1', :body => 'Article to test profile search', :profile => person)
    article2 = TextArticle.create(:name => 'Article 2', :body => 'Another article to test profile search', :profile => person)

    get 'index', :profile => person.identifier, :q => ''

    assert_no_tag :tag => 'ul', :attributes => { :id => 'profile-search-results'}, :descendant => { :tag => 'li' }
  end

  should 'not display private articles' do
    article1 = TextArticle.create(:name => 'Article 1', :body => '<p>Article to test profile search</p>', :profile => person, :published => false)
    article2 = TextArticle.create(:name => 'Article 2', :body => '<p>Another article to test profile search</p>', :profile => person)

    get 'index', :profile => person.identifier, :q => 'article to test'

    assert_no_tag :tag => 'li', :descendant => { :tag => 'a', :content => 'Article to test profile search', :attributes => { :class => /article-details/ }}

    assert_tag :tag => 'li', :descendant => { :tag => 'a', :content => 'Another article to test profile search', :attributes => { :class => /article-details/ }}
  end

  should 'display number of results found' do
    article1 = TextArticle.create(:name => 'Article 1', :body => 'Article to test profile search', :profile => person)
    article2 = TextArticle.create(:name => 'Article 2', :body => 'Another article to test profile search', :profile => person)

    get 'index', :profile => person.identifier, :q => 'article to test'

    assert_tag :tag => 'div', :attributes => { :class => 'results-found-message' }, :content => /2 results found/
  end

  should 'search on article name and content' do
    article1 = TextileArticle.create(:name => 'Article Query', :body => 'Article to test profile search on article name', :profile => person)
    article2 = TextileArticle.create(:name => 'Article 2', :body => 'Another article to test profile search for query on article content', :profile => person)

    get 'index', :profile => person.identifier, :q => 'query'

    assert_tag :tag => 'div', :attributes => { :class => 'results-found-message' }, :content => /2 results found/
  end

  should 'display result found equivalent to items listed' do
    article1 = TextileArticle.create(:name => 'Article 1', :body => 'Article to test profile search results', :profile => person)
    article2 = TextileArticle.create(:name => 'Article 2', :body => 'Article to test profile search results', :profile => person)
    article3 = TextileArticle.create(:name => 'Article 3', :body => 'Article to test profile search results', :profile => person)

    get 'index', :profile => person.identifier, :q => 'test'

    assert_tag :tag => 'ul', :attributes => { :class => 'results-list'},
      :children => { :count => 3, :only => { :tag => "li" } }
  end
end