require 'test_helper' class ArticleTest < ActiveSupport::TestCase context "for basic article tests" do should "create valid article" do assert_difference('Article.count') do article = Article.make assert article.valid? end end should "create invalid article, the URL is invalid" do article = Article.make_unsaved(:url => Sham.incorrect_url) assert !article.valid? end should "create duplicate article, should fail as the guid is the same" do article = Article.make article_duplicate = Article.make_unsaved(:guid => article.guid) assert !article_duplicate.valid? end should "modify valid article, should fail when the URL changed to invalid" do article = Article.make article.url = Sham.incorrect_url assert !article.valid? end end context "for sunspot" do setup { Sunspot.remove_all! } context "on indexing and searching central-european characters" do setup do 10.times { Article.make } [:accent_in_title, :accent_in_summary, :accent_in_body].each { |i| Article.make(i) } Sunspot.commit end should "find articles with accents" do search = Sunspot.search(Article) { keywords Sham.accented_word } assert_equal 3, search.results.count end should "find articles with no accents" do search = Sunspot.search(Article) { keywords Sham.unaccented_word } assert_equal 3, search.results.count end end context "on indexing characters prohibited in XML documents" do should "not raise expcetion" do assert_nothing_raised { Article.make(:with_corrupt_xml_chars) } end end context "on article status" do setup do @user = User.make @article = Article.make @article.set_user_status(@user, ArticleStatus::STATUS_SENT) Sunspot.commit end should "change the status of the article for the user" do assert_equal @user, @article.article_statuses.first.user end should "not find the article if the status is excluded from the search" do query = @article.title self_user = @user search = Sunspot.search(Article) do keywords query dynamic :status do without Article.give_user_status_symbol(self_user, ArticleStatus::STATUS_SENT), true end end assert_equal 0, search.results.count end should "find the article if the status is included to the search" do query = @article.title self_user = @user search = Sunspot.search(Article) do keywords query dynamic :status do with Article.give_user_status_symbol(self_user, ArticleStatus::STATUS_SENT), true end end assert_equal 1, search.results.count end end end end