aboutsummaryrefslogtreecommitdiffstats
path: root/spec/features/refinery/blog/admin/posts_spec.rb
blob: 99ec25d7b68d6f60baad28b0ee65ac13c7707d64 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# encoding: utf-8
require "spec_helper"

module Refinery
  module Blog
    module Admin
      describe Post, type: :feature do
        refinery_login_with :refinery_user

        let!(:blog_category) do
          Globalize.with_locale(:en) { FactoryGirl.create(:blog_category) }
        end

        context "when no blog posts" do
          before { subject.class.destroy_all }

          describe "blog post listing" do
            before { visit refinery.blog_admin_posts_path }

            it "invites to create new post" do
              expect(page).to have_content("There are no Blog Posts yet. Click \"Create new post\" to add your first blog post.")
            end
          end

          describe "new blog post form" do
            before do
              visit refinery.blog_admin_posts_path
              click_link "Create new post"
            end

            it "should have Tags" do
              expect(page).to have_content("Tags")
            end

            it "should have category title", :js => true do
              find('#toggle_advanced_options').click
              expect(page).to have_content(blog_category.title)
            end

            describe "create blog post", :js => true do
              before do
                expect(subject.class.count).to eq(0)
                fill_in "post_title", :with => "This is my blog post"

                # this is a dirty hack but textarea that needs to be filled is
                # hidden and capybara refuses to fill in elements it can't see
                page.evaluate_script("WYMeditor.INSTANCES[0].html('<p>And I love it</p>')")
                find('#toggle_advanced_options').click
                expect(page).to have_css '.blog_categories'
                expect(page).to have_css "#post_category_ids_#{blog_category.id}"
                expect(page).to have_selector("#post_category_ids_#{blog_category.id}:not(:checked)")
                check blog_category.title
                expect(page).to have_selector("#post_category_ids_#{blog_category.id}:checked")
                click_button "Save"
                sleep 1
                expect(page).to have_content("was successfully added.")
              end

              it "should be the only blog post" do
                expect(subject.class.count).to eq(1)
              end

              it "should belong to me" do
                expect(subject.class.first.author).to eq(::Refinery::User.last)
              end

              it "should save categories" do
                expect(subject.class.last.categories.count).to eq(1)
                expect(subject.class.last.categories.first.title).to eq(blog_category.title)
              end
            end

            describe "create blog post with tags", :js => true do
              let(:tag_list) { "chicago, bikes, beers, babes" }
              before do
                fill_in "Title", :with => "This is a tagged blog post"
                # this is a dirty hack but textarea that needs to be filled is
                # hidden and capybara refuses to fill in elements it can't see
                page.evaluate_script("WYMeditor.INSTANCES[0].html('<p>And I also love it</p>')")
                fill_in "Tags", :with => tag_list
                click_button "Save"
              end

              it "should succeed" do
                expect(page).to have_content("was successfully added.")
              end

              it "should be the only blog post" do
                expect(subject.class.count).to eq(1)
              end

              it "should have the specified tags" do
                expect(subject.class.last.tag_list.sort).to eq(tag_list.split(', ').sort)
              end
            end
          end
        end

        context "when has blog posts" do
          let!(:blog_post) do
            Globalize.with_locale(:en) { FactoryGirl.create(:blog_post) }
          end

          describe "blog post listing" do
            before { visit refinery.blog_admin_posts_path }

            describe "edit blog post" do
              it "should succeed" do
                expect(page).to have_content(blog_post.title)

                click_link("Edit this blog post")
                expect(current_path).to eq(refinery.edit_blog_admin_post_path(blog_post))

                fill_in "post_title", :with => "hax0r"
                click_button "Save"

                expect(page).not_to have_content(blog_post.title)
                expect(page).to have_content("'hax0r' was successfully updated.")
              end
            end

            describe "deleting blog post" do
              it "should succeed" do
                expect(page).to have_content(blog_post.title)

                click_link "Remove this blog post forever"

                expect(page).to have_content("'#{blog_post.title}' was successfully removed.")
              end
            end

            describe "view live" do
              it "redirects to blog post in the frontend" do
                click_link "View this blog post live"

                expect(current_path).to eq(refinery.blog_post_path(blog_post))
                expect(page).to have_content(blog_post.title)
              end
            end
          end

          context "when uncategorized post" do
            it "shows up in the list" do
              visit refinery.uncategorized_blog_admin_posts_path
              expect(page).to have_content(blog_post.title)
            end
          end

          context "when categorized post" do
            it "won't show up in the list" do
              blog_post.categories << blog_category
              blog_post.save!

              visit refinery.uncategorized_blog_admin_posts_path
              expect(page).not_to have_content(blog_post.title)
            end
          end
        end

        context "with multiple users" do
          let!(:other_guy) { FactoryGirl.create(:refinery_user, :username => "Other Guy") }

          describe "create blog post with alternate author", :js => true do
            before do
              visit refinery.blog_admin_posts_path
              click_link "Create new post"

              fill_in "post_title", :with => "This is some other guy's blog post"
              # this is a dpage_titleirty hack but textarea that needs to be filled is
              # hidden and capybara refuses to fill in elements it can't see
              page.evaluate_script("WYMeditor.INSTANCES[0].html('<p>I totally did not write it.</p>')")

              find('a', text: 'Toggle Advanced Options').trigger(:click)
              expect(page).to have_content("Author")
              select other_guy.username, :from => "Author"

              click_button "Save"
              expect(page).to have_content("was successfully added.")
            end

            it "belongs to another user" do
              expect(subject.class.last.author).to eq(other_guy)
            end
          end
        end

        context "with translations" do
          before do
            Globalize.locale = :en
            allow(Refinery::I18n).to receive(:frontend_locales).and_return([:en, :ru])
            blog_page = FactoryGirl.create(:page, :link_url => "/blog", :title => "Blog")
            Globalize.with_locale(:ru) do
              blog_page.title = 'блог'
              blog_page.save
            end
            visit refinery.blog_admin_posts_path
          end

          describe "add a blog post with title for default locale" do
            before do
              click_link "Create new post"
              fill_in "Title", :with => "Post"
              fill_in "post_body", :with => "One post in my blog"
              click_button "Save"
              @p = Refinery::Blog::Post.by_title("Post")
            end

            it "succeeds" do
              expect(page).to have_content("'Post' was successfully added.")
              expect(Refinery::Blog::Post.count).to eq(1)
            end

            it "shows locale for post" do

              within "#post_#{@p.id}" do
                expect(page).to have_css(".locale_icon.en")
              end
            end

            it "shows up in blog page for default locale" do
              visit refinery.blog_root_path
              expect(page).to have_selector("#post_#{@p.id}")
            end

            it "does not show up in blog page for secondary locale" do
              visit refinery.blog_root_path(:locale => :ru)
              expect(page).not_to have_selector("#post_#{@p.id}")
            end

          end

          describe "add a blog post with title only for secondary locale" do

            let(:ru_page_title) { 'Новости' }

            before do
              click_link "Create new post"
              within "#switch_locale_picker" do
                click_link "RU"
              end
              fill_in "Title", :with => ru_page_title
              fill_in "post_body", :with => "One post in my blog"
              click_button "Save"
              @p = Refinery::Blog::Post.by_title(ru_page_title)
            end

            it "succeeds" do
              expect(page).to have_content("was successfully added.")
              expect(Refinery::Blog::Post.count).to eq(1)
            end

            it "shows title in secondary locale" do
              within "#post_#{@p.id}" do
                expect(page).to have_content(ru_page_title)
              end
            end

            it "shows locale for post" do
              within "#post_#{@p.id}" do
                expect(page).to have_css(".locale_icon.ru")
              end
            end

            it "does not show locale for primary locale" do
              within "#post_#{@p.id}" do
                expect(page).not_to have_css(".locale_icon.en")
              end
            end

            it "does not show up in blog page for default locale" do
              visit refinery.blog_root_path
              expect(page).not_to have_selector("#post_#{@p.id}")
            end

            it "shows up in blog page for secondary locale" do
              visit refinery.blog_root_path(:locale => :ru)
              expect(page).to have_selector("#post_#{@p.id}")
            end

          end

          context "with a blog post in both locales" do

            let!(:blog_post) do
              _blog_post = Globalize.with_locale(:en) { FactoryGirl.create(:blog_post, :title => 'First Post') }
              Globalize.with_locale(:ru) do
                _blog_post.title = 'Домашняя страница'
                _blog_post.save
              end
              _blog_post
            end

            before do
              visit refinery.blog_admin_posts_path
            end

            it "shows both locales for post" do
              within "#post_#{blog_post.id}" do
                expect(page).to have_css(".locale_icon.en")
                expect(page).to have_css(".locale_icon.ru")
              end
            end

            describe "edit the post in english" do
              it "succeeds" do

                within "#post_#{blog_post.id}" do
                  click_link("EN")
                end
                expect(current_path).to eq(refinery.edit_blog_admin_post_path(blog_post))
                fill_in "Title", :with => "New Post Title"
                click_button "Save"

                expect(page).not_to have_content(blog_post.title)
                expect(page).to have_content("'New Post Title' was successfully updated.")
              end
            end

            describe "edit the post in secondary locale" do
              it "succeeds" do
                within "#post_#{blog_post.id}" do
                  click_link("RU")
                end

                fill_in "Title", :with => "Нов"
                click_button "Save"

                expect(page).not_to have_content(blog_post.title)
                expect(page).to have_content("'Нов' was successfully updated.")
              end
            end

            describe "delete the post translation in secondary locale" do
              it "succeeds" do
                within "#post_#{blog_post.id}" do
                  click_link("RU")
                end

                click_link "Remove this translation"

                expect(page).not_to have_content(blog_post.title)
                expect(page).to have_content("The translation was successfully removed.")
              end
            end
          end
        end

      end
    end
  end
end