Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions spec/factories/articles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
end
end

# TODO: create a published trait that set `published_at` and the
# `publication_status`, so the draft trait can properly set `published_at` to
# nil
trait(:draft) do
published_at { nil }
publication_status { 'draft' }
# TODO: should we also put `published_at_tz { "UTC" }` ?
end

trait(:arabic) { locale { 'ar' } }
trait(:bengali) { locale { 'bn' } }
trait(:czech) { locale { 'cs' } }
Expand Down
83 changes: 83 additions & 0 deletions spec/requests/article_datetime_settings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require 'rails_helper'

# rubocop:disable RSpec/DescribeClass
RSpec.describe 'admin article controller set_published_at before_action set published_at' do
include ActiveSupport::Testing::TimeHelpers

let(:draft_attrs) { attributes_for(:article, :draft, published_at_tz: 'UTC', published_at: nil).compact }
let(:published_attrs) { attributes_for(:article, publication_at_tz: 'UTC', published_at: nil).compact }
let(:request_params) { { button: '', controller: 'admin/articles' } }

shared_examples 'handles the publication date' do |user_role|
subject(:actual_published_at) { Article.last.published_at.to_s }

let(:user) { create(:user, password: 'c' * 31, role: user_role) }
let(:now) { Time.now.utc }

before do
post sessions_url, params: { username: user.username, password: user.password }
end

context "when creating an article record with role: #{user_role}" do
before do
travel_to(now) do
post admin_articles_url, params: { article: article_attrs, action: 'create', **request_params }
end
end

it { is_expected.to eq expected_published_at }
end

context "when updating an existing article record with role: #{user_role}" do
before do
travel_to(now) do
put admin_article_url(existing_article.id),
params: { article: article_attrs, action: 'update', **request_params }
end
end

it { is_expected.to eq expected_published_at }
end
end

context 'with valid publication info' do
let(:request_params) { super().merge(published_at_date: '2018-12-24', published_at_time: '11:59:00') }
let(:article_attrs) { draft_attrs.merge(published_at: DateTime.parse('2018-12-25T03:59:00Z')) }
let(:existing_article) { create(:article, :draft, published_at: DateTime.parse('2018-12-25T03:59:00Z')) }

it_behaves_like 'handles the publication date', :author do
let(:expected_published_at) { '2018-12-24 11:59:00 UTC' }
end
end

context 'when the publication info is missing' do
let(:request_params) { super().merge(published_at_date: nil, published_at_time: nil) }

describe 'when the article is marked as published' do
let(:existing_article) { create(:article) }
let(:article_attrs) { published_attrs.merge(published_at: nil) }

it_behaves_like 'handles the publication date', :publisher do
let(:expected_published_at) { (now.utc + 100.years).to_s }
end

it_behaves_like 'handles the publication date', :author do
let(:expected_published_at) { '' }
end
end

describe 'when the article is still a draft' do
let(:existing_article) { create(:article, :draft) }
let(:article_attrs) { draft_attrs.merge(published_at: nil) }

it_behaves_like 'handles the publication date', :publisher do
let(:expected_published_at) { '' }
end

it_behaves_like 'handles the publication date', :author do
let(:expected_published_at) { '' }
end
end
end
end
# rubocop:enable RSpec/DescribeClass
84 changes: 0 additions & 84 deletions spec/system/article_datetime_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,6 @@
create(:user, username: 'user1', password: 'c' * 31, role: 'publisher')
end

it 'creates a new article' do
login_user
visit '/admin/articles'

click_link_or_button 'NEW'

within '#publication_datetime' do
execute_script("document.getElementById('publication_date').value = '2018-12-24';")
execute_script("document.getElementById('publication_time').value = '11:59:00';")
select('UTC', from: 'article_published_at_tz')
end

within('#publication-status') { find('label[for=publication_status_draft]').click }

find_button('Save', match: :first).click

expect(page).to have_text 'Article was successfully created'
article = Article.first
expect(article.published_at.utc).to eq('2018-12-24 11:59:00 UTC')
end

it 'updates an existing article' do
article = create(:article, published_at: Time.zone.parse('2018-12-24 11:59:00 UTC'))
expect(article.published_at.utc).to eq('2018-12-24 11:59:00 UTC')
expect(article.published_at_tz).to eq('Pacific Time (US & Canada)')

login_user
visit '/admin/articles'

click_link_or_button 'EDIT'
within '#publication_datetime' do
# make sure pre-fills are right
expect(find_field('published_at_date').value).to eq '2018-12-24'
expect(find_field('published_at_time').value).to eq '03:59:00'
expect(find_field('article_published_at_tz').value).to eq 'Pacific Time (US & Canada)'

execute_script("document.getElementById('publication_date').value = '2018-12-26';")
execute_script("document.getElementById('publication_time').value = '22:59:00';")
select('UTC', from: 'article_published_at_tz')
end

within('#publication-status') { find('label[for=publication_status_draft]').click }

find_button('Save', match: :first).click

expect(page).to have_text 'Article was successfully updated'
expect(article.reload.published_at.utc).to eq('2018-12-26 22:59:00 UTC')
expect(article.reload.published_at_tz).to eq('UTC')
end

it 'saves an article without entering publication date info' do
login_user
visit '/admin/articles'

click_link_or_button 'NEW'

within('#publication-status') { find('label[for=publication_status_draft]').click }

find_button('Save', match: :first).click

expect(page).to have_text 'Article was successfully created'
article = Article.first
expect(article.published_at).to be_nil
end

it 'uses ‘PUBLISH NOW’ feature' do
freeze_time do
login_user
Expand All @@ -90,23 +25,4 @@
expect(article).to be_published
end
end

it 'sets the publication date/time if article is `published` and fields are blank' do
freeze_time do
login_user
visit '/admin/articles'

click_link_or_button 'NEW'

within('#publication-status') { find('label[for=publication_status_published]').click }
find_button('Save', match: :first).click

expect(page).to have_text 'Article was successfully created'
article = Article.last

expect(article.reload.published_at_tz).to eq('UTC')
expect(article.published_at).to eq(Time.now.utc + 100.years)
expect(article).to be_published
end
end
end
Loading