@@ -46,4 +46,63 @@ class SearchControllerTest < ActionController::TestCase
4646 end
4747 end
4848
49+ test 'should restrict searches to current space' do
50+ plant_space = spaces ( :plants )
51+ # Dummy class that records which `with` constraints were used in the search
52+ dummy_search = Class . new do
53+ attr_reader :constraints
54+
55+ def initialize
56+ @constraints = Hash . new ( :unset )
57+ end
58+
59+ def clear
60+ @constraints . clear
61+ end
62+
63+ def with ( field , value = nil )
64+ @constraints [ field ] = value
65+ self
66+ end
67+
68+ def method_missing ( *args ) ; self ; end
69+ end
70+
71+ search = dummy_search . new
72+ Sunspot . stub ( :search , -> ( _ , &block ) { search . instance_eval ( &block ) } ) do
73+ # When not in a space, with spaces disabled, space constraint should be unset (show everything)
74+ with_settings ( solr_enabled : true , feature : { spaces : false } ) do
75+ get :index , params : { q : 'banana' }
76+ assert_response :success
77+ assert_equal :unset , search . constraints [ :space_id ]
78+ end
79+
80+ search . clear
81+ # When not in a space, with spaces enabled, space constraint should be nil (show things in default space)
82+ with_settings ( solr_enabled : true , feature : { spaces : true } ) do
83+ get :index , params : { q : 'banana' }
84+ assert_response :success
85+ assert_nil search . constraints [ :space_id ]
86+ end
87+
88+ with_host ( plant_space . host ) do
89+ search . clear
90+ # When in a space, with spaces enabled, space constraint should be that space's ID (show things in that space)
91+ with_settings ( solr_enabled : true , feature : { spaces : true } ) do
92+ get :index , params : { q : 'banana' }
93+ assert_response :success
94+ assert_equal plant_space . id , search . constraints [ :space_id ]
95+ end
96+
97+ search . clear
98+ # When in a space, with spaces disabled, space constraint should be unset (show everything)
99+ with_settings ( solr_enabled : true , feature : { spaces : false } ) do
100+ get :index , params : { q : 'banana' }
101+ assert_response :success
102+ assert_equal :unset , search . constraints [ :space_id ]
103+ end
104+ end
105+ end
106+ end
107+
49108end
0 commit comments