aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/general_controller_spec.rb
blob: 63ad6873cf8bd0445025d189b66f86fff98c17c1 (plain)
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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe GeneralController, "when searching" do
    integrate_views
    fixtures [ :info_requests,
               :info_request_events,
               :public_bodies,
               :public_body_translations,
               :users,
               :raw_emails,
               :outgoing_messages,
               :incoming_messages,
               :comments ]

    it "should render the front page successfully" do
        get :frontpage
        response.should be_success
    end

    it "should render the front page with default language" do
        get :frontpage
        response.should have_tag('html[lang="en"]')
    end

    it "should render the front page with default language" do
        old_default_locale = I18n.default_locale
        I18n.default_locale = "es"
        get :frontpage
        response.should have_tag('html[lang="es"]')
        I18n.default_locale = old_default_locale
    end

    it "should render the front page with browser-selected language" do
        accept_language = "es-ES,en-GB,en-US;q=0.8,en;q=0.6"
        request.env['HTTP_ACCEPT_LANGUAGE'] = accept_language
        get :frontpage
        response.should have_tag('html[lang="es"]')
        request.env['HTTP_ACCEPT_LANGUAGE'] = nil
    end

    it "doesn't raise an error when there's no user matching the one in the session" do
        session[:user_id] = 999
        get :frontpage
        response.should be_success
    end


    it "should redirect from search query URL to pretty URL" do
        post :search_redirect, :query => "mouse" # query hidden in POST parameters
        response.should redirect_to(:action => 'search', :combined => "mouse") # URL /search/:query
    end

    describe "when using different locale settings" do 
        home_link_regex = /href=".*\/en"/
        it "should generate URLs with a locale prepended when there's more than one locale set" do
            ActionController::Routing::Routes.add_filters(['conditionallyprependlocale'])
            get :frontpage
            response.should have_text(home_link_regex)
        end

        it "should generate URLs without a locale prepended when there's only one locale set" do
            ActionController::Routing::Routes.add_filters(['conditionallyprependlocale'])
            old_available_locales =  FastGettext.default_available_locales
            available_locales = ['en']
            FastGettext.default_available_locales = available_locales
            I18n.available_locales = available_locales

            get :frontpage
            response.should_not have_text(home_link_regex)

            FastGettext.default_available_locales = old_available_locales
            I18n.available_locales = old_available_locales
        end
    end

    describe 'when using xapian search' do

      # rebuild xapian index after fixtures loaded
      before(:all) do
          rebuild_xapian_index
      end

      it "should find info request when searching for '\"fancy dog\"'" do
          get :search, :combined => ['"fancy dog"']
          response.should render_template('search')
          assigns[:xapian_requests].matches_estimated.should == 1
          assigns[:xapian_requests].results.size.should == 1
          assigns[:xapian_requests].results[0][:model].should == info_request_events(:useless_outgoing_message_event)

          assigns[:xapian_requests].words_to_highlight == ["fancy", "dog"]
      end

      it "should find public body and incoming message when searching for 'geraldine quango'" do
          get :search, :combined => ['geraldine quango']
          response.should render_template('search')

          assigns[:xapian_requests].matches_estimated.should == 1
          assigns[:xapian_requests].results.size.should == 1
          assigns[:xapian_requests].results[0][:model].should == info_request_events(:useless_incoming_message_event)

          assigns[:xapian_bodies].matches_estimated.should == 1
          assigns[:xapian_bodies].results.size.should == 1
          assigns[:xapian_bodies].results[0][:model].should == public_bodies(:geraldine_public_body)
      end

    end

    it "should show help when searching for nothing" do
        get :search_redirect, :query => nil
        response.should render_template('search')
        assigns[:total_hits].should be_nil
        assigns[:query].should be_nil
    end


end