docbrown/spec/services/search/article_spec.rb
Anna Buianova ff76cdd3c5
Scheduling articles (#17939)
* Article query spec for scheduled articles

* Added scheduled article badge on the user dashboard

* Added published_at field to editor options

* Accept and validate published_at from editor

* Refactor published_at validation

* Allow 1-minute difference in published_at

* Notice on an unpublished article page

* Added specs for 'Click to edit' link on scheduled article preview page

* ContextNotification model

* Articles::Publish worker

* Added specs for articles publish worker

* Schedule publish articles worker

* Added tests to check for scheduled posts in feeds

* Don't allow managing scheduled articles

* Don't send notifications for scheduled articles

* Set published_at in Articles::Updater when publishing

* Published_at value in post options

* Pass timezone and set published_at accordingly

* Limit setting published_at to the future

* Readonly published_at for articles that were already published

* Chagning published_at format in editor v1 (start)

* Changed published_at format in frontmatter, specs

* Added specs for updating published_at from frontmatter

* Fixed accepting past published_at for articles published_from_feed

* Enabled published_at validation: don't allow updating published_at for already published articles

* Validate published_at on create

* Added a spec for updating published_at for exported articles

* Fixed specs related to creating articles with past published_at

* Fixed specs related to past published_at for articles

* Added a hack so that admins would be able to update published_at

* Switch button text schedule/publish when changin publishedAt

* Fixed saving published_at with timezone

* Added a feature flag for scheduling articles

* Default text in markdown editor depends on feature flag

* Enable article editor cache again

* Fixed the default value in the markdown editor

* Fix sitemaps spec

* Removed tooltip

* Fixed articles update specs

* Added missing locales

* Fixed article create specs

* Fixed spec

* Removed commented code

* Returned enabling extensions in the schema

* Returned accidentally deleted constraint

* Make articles query spec more stable

Co-authored-by: Jeremy Friesen <jeremy.n.friesen@gmail.com>

* Removed commented code

* Removed unused code

* A clearer policy

Co-authored-by: Jeremy Friesen <jeremy.n.friesen@gmail.com>

* Use StringInquirer for article current state

* Added a note and todo to articles factory past trait

* Remove duplicated PropType

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Refactor query in the Articles::PublishWorker

Co-authored-by: Mac Siri <krairit.siri@gmail.com>

* Refactor articleForm.jsx

Co-authored-by: Mac Siri <krairit.siri@gmail.com>

* Removed specs that are no longer relevant

* Removed useless onKeyUp on a hidden input

* Refactored articleForm

* Hide scheduling from post options when published_at is readonly

* Run sends notifications worker every 5 minutes instead of every minute

Co-authored-by: Jeremy Friesen <jeremy.n.friesen@gmail.com>
Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
Co-authored-by: Mac Siri <krairit.siri@gmail.com>
2022-07-07 17:32:49 +03:00

171 lines
6.5 KiB
Ruby

require "rails_helper"
# rubocop:disable Rails/PluckId
# This spec uses `pluck` on an array of hashes, but Rubocop can't tell the difference.
RSpec.describe Search::Article, type: :service do
describe "::search_documents" do
it "returns an empty result if there are no articles" do
expect(described_class.search_documents).to be_empty
end
it "does not return unpublished articles" do
article = create(:article, published: false)
expect(described_class.search_documents.pluck(:id)).not_to include(article.id)
end
it "returns published articles" do
article = create(:article)
expect(described_class.search_documents.pluck(:id)).to include(article.id)
end
context "when describing the result format" do
it "does not include a highlight attribute" do
create(:article)
expect(described_class.search_documents.first.key?(:highlight)).to be(false)
end
end
context "when filtering by user_id" do
it "returns articles belonging to a specific user", :aggregate_failures do
user = create(:user)
articles = create_list(:article, 2, user: user)
article = create(:article)
results = described_class.search_documents(user_id: user.id).pluck(:id)
expect(results).not_to include(article.id)
expect(results).to match_array(articles.pluck(:id))
end
end
context "when searching for a term" do
let(:article) { create(:article) }
it "matches against the article's body_markdown", :aggregate_failures do
article.update_columns(body_markdown: "Life of the party")
result = described_class.search_documents(term: "part").first
expect(result[:path]).to eq(article.path)
results = described_class.search_documents(term: "fiesta")
expect(results).to be_empty
end
it "matches against the article's title", :aggregate_failures do
article.update_columns(title: "Life of the party")
result = described_class.search_documents(term: "part").first
expect(result[:path]).to eq(article.path)
results = described_class.search_documents(term: "fiesta")
expect(results).to be_empty
end
it "matches against the article's tags", :aggregate_failures do
article.update_columns(cached_tag_list: "javascript, beginners, ruby")
result = described_class.search_documents(term: "beginner").first
expect(result[:path]).to eq(article.path)
results = described_class.search_documents(term: "newbie")
expect(results).to be_empty
end
it "matches against the article's organization's name", :aggregate_failures do
article.organization = create(:organization, name: "ACME corp")
article.save!
result = described_class.search_documents(term: "ACME").first
expect(result[:path]).to eq(article.path)
results = described_class.search_documents(term: "ECMA")
expect(results).to be_empty
end
it "matches against the article's user's name", :aggregate_failures do
result = described_class.search_documents(term: article.user_name.first(3)).first
expect(result[:path]).to eq(article.path)
results = described_class.search_documents(term: "notaname")
expect(results).to be_empty
end
it "matches against the article's user's username", :aggregate_failures do
result = described_class.search_documents(term: article.user_username.first(3)).first
expect(result[:path]).to eq(article.path)
results = described_class.search_documents(term: "notausername")
expect(results).to be_empty
end
end
context "when sorting" do
it "sorts by title, tags, body ranking by default with a search term", :aggregate_failures do
article_body = create(:article)
article_body.update(body_markdown: "The Other Side of Silence Impedit consequatur")
article_tag = create(:article, tags: "consequatur")
article_title = create(:article, title: "The Other Side of Silence Impedit consequatur")
results = described_class.search_documents(term: "consequatur")
expect(results.pluck(:id)).to eq([article_title.id, article_tag.id, article_body.id])
end
it "sorts by 'hotness_score' and 'comments_count' in descending order without a search term" do
article1, article2, article3 = create_list(:article, 3)
article1.update_columns(hotness_score: 10, comments_count: 10)
article2.update_columns(hotness_score: 10, comments_count: 11)
article3.update_columns(hotness_score: 9, comments_count: 20)
results = described_class.search_documents
expect(results.pluck(:id)).to eq([article2.id, article1.id, article3.id])
end
it "supports sorting by published_at in ascending and descending order with a search term", :aggregate_failures do
article1 = create(:article, tags: "ruby")
article2 = create(:article, :past, tags: "ruby", past_published_at: 1.week.ago)
results = described_class.search_documents(term: "ruby", sort_by: :published_at, sort_direction: :asc)
expect(results.pluck(:id)).to eq([article2.id, article1.id])
results = described_class.search_documents(term: "ruby", sort_by: :published_at, sort_direction: :desc)
expect(results.pluck(:id)).to eq([article1.id, article2.id])
end
it "supports sorting by published_at in ascending and descending order without a search term",
:aggregate_failures do
article1 = create(:article)
article2 = create(:article, :past, past_published_at: 1.week.ago)
results = described_class.search_documents(sort_by: :published_at, sort_direction: :asc)
expect(results.pluck(:id)).to eq([article2.id, article1.id])
results = described_class.search_documents(sort_by: :published_at, sort_direction: :desc)
expect(results.pluck(:id)).to eq([article1.id, article2.id])
end
end
context "when paginating" do
it "returns no items when out of pagination bounds" do
create(:article)
result = described_class.search_documents(page: 99)
expect(result).to be_empty
end
it "returns paginated items", :aggregate_failures do
create_list(:article, 2)
result = described_class.search_documents(page: 0, per_page: 1)
expect(result.length).to eq(1)
result = described_class.search_documents(page: 1, per_page: 1)
expect(result.length).to eq(1)
end
end
end
end
# rubocop:enable Rails/PluckId