aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test/actionpack/controller/view_paths_test.rb
blob: 4449d08496c9c87948c4e5eccb3031c3e4c2ae27 (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# frozen_string_literal: true

require "abstract_unit"

class ViewLoadPathsTest < ActionController::TestCase
  class TestController < ActionController::Base
    def self.controller_path() "test" end

    before_action :add_view_path, only: :hello_world_at_request_time

    def hello_world() end
    def hello_world_at_request_time() render(action: "hello_world") end

    private
      def add_view_path
        prepend_view_path "#{FIXTURE_LOAD_PATH}/override"
      end
  end

  module Test
    class SubController < ActionController::Base
      layout "test/sub"
      def hello_world; render(template: "test/hello_world"); end
    end
  end

  with_routes do
    get :hello_world, to: "test#hello_world"
    get :hello_world_at_request_time, to: "test#hello_world_at_request_time"
  end

  def setup
    @controller = TestController.new
    @request  = ActionController::TestRequest.create(@controller.class)
    @response = ActionDispatch::TestResponse.new
    @paths = TestController.view_paths
    super
  end

  def teardown
    TestController.view_paths = @paths
  end

  def expand(array)
    array.map { |x| File.expand_path(x.to_s) }
  end

  def assert_paths(*paths)
    controller = paths.first.is_a?(Class) ? paths.shift : @controller
    assert_equal expand(paths), controller.view_paths.map(&:to_s)
  end

  def test_template_load_path_was_set_correctly
    assert_paths FIXTURE_LOAD_PATH
  end

  def test_controller_appends_view_path_correctly
    @controller.append_view_path "foo"
    assert_paths(FIXTURE_LOAD_PATH, "foo")

    @controller.append_view_path(%w(bar baz))
    assert_paths(FIXTURE_LOAD_PATH, "foo", "bar", "baz")

    @controller.append_view_path(FIXTURE_LOAD_PATH)
    assert_paths(FIXTURE_LOAD_PATH, "foo", "bar", "baz", FIXTURE_LOAD_PATH)
  end

  def test_controller_prepends_view_path_correctly
    @controller.prepend_view_path "baz"
    assert_paths("baz", FIXTURE_LOAD_PATH)

    @controller.prepend_view_path(%w(foo bar))
    assert_paths "foo", "bar", "baz", FIXTURE_LOAD_PATH

    @controller.prepend_view_path(FIXTURE_LOAD_PATH)
    assert_paths FIXTURE_LOAD_PATH, "foo", "bar", "baz", FIXTURE_LOAD_PATH
  end

  def test_template_appends_view_path_correctly
    @controller.instance_variable_set :@template, ActionView::Base.with_view_paths(TestController.view_paths, {}, @controller)
    class_view_paths = TestController.view_paths

    @controller.append_view_path "foo"
    assert_paths FIXTURE_LOAD_PATH, "foo"

    @controller.append_view_path(%w(bar baz))
    assert_paths FIXTURE_LOAD_PATH, "foo", "bar", "baz"
    assert_paths TestController, *class_view_paths
  end

  def test_template_prepends_view_path_correctly
    @controller.instance_variable_set :@template, ActionView::Base.with_view_paths(TestController.view_paths, {}, @controller)
    class_view_paths = TestController.view_paths

    @controller.prepend_view_path "baz"
    assert_paths "baz", FIXTURE_LOAD_PATH

    @controller.prepend_view_path(%w(foo bar))
    assert_paths "foo", "bar", "baz", FIXTURE_LOAD_PATH
    assert_paths TestController, *class_view_paths
  end

  def test_view_paths
    get :hello_world
    assert_response :success
    assert_equal "Hello world!", @response.body
  end

  def test_view_paths_override
    TestController.prepend_view_path "#{FIXTURE_LOAD_PATH}/override"
    get :hello_world
    assert_response :success
    assert_equal "Hello overridden world!", @response.body
  end

  def test_view_paths_override_for_layouts_in_controllers_with_a_module
    @controller = Test::SubController.new
    with_routes do
      get :hello_world, to: "view_load_paths_test/test/sub#hello_world"
    end

    Test::SubController.view_paths = [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH, "#{FIXTURE_LOAD_PATH}/override2" ]
    get :hello_world
    assert_response :success
    assert_equal "layout: Hello overridden world!", @response.body
  end

  def test_view_paths_override_at_request_time
    get :hello_world_at_request_time
    assert_response :success
    assert_equal "Hello overridden world!", @response.body
  end

  def test_decorate_view_paths_with_custom_resolver
    decorator_class = Class.new(ActionView::PathResolver) do
      def initialize(path_set)
        @path_set = path_set
      end

      def find_all(*args)
        @path_set.find_all(*args).collect do |template|
          ::ActionView::Template.new(
            "Decorated body",
            template.identifier,
            template.handler,
            virtual_path: template.virtual_path,
            format: template.format,
            locals: template.locals
          )
        end
      end
    end

    decorator = decorator_class.new(TestController.view_paths)
    TestController.view_paths = ActionView::PathSet.new.push(decorator)

    get :hello_world
    assert_response :success
    assert_equal "Decorated body", @response.body
  end

  def test_inheritance
    original_load_paths = ActionController::Base.view_paths

    self.class.class_eval %{
      class A < ActionController::Base; end
      class B < A; end
      class C < ActionController::Base; end
    }

    A.view_paths = ["a/path"]

    assert_paths A, "a/path"
    assert_paths A, *B.view_paths
    assert_paths C, *original_load_paths

    C.view_paths = []
    assert_nothing_raised { C.append_view_path "c/path" }
    assert_paths C, "c/path"
  end

  def test_lookup_context_accessor
    assert_equal ["test"], TestController.new.lookup_context.prefixes
  end
end