<commit_before>require 'spec_helper'

describe Alchemy::PagesController do
	
	before(:each) do
		@default_language = Alchemy::Language.get_default
		@default_language_root = Factory(:language_root_page, :language => @default_language, :name => 'Home')
	end

	describe "#show" do

		it "should include all its elements and contents" do
			p = Factory(:public_page, :language => @default_language)
			article = p.elements.find_by_name('article')
			article.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
			visit '/alchemy/a-public-page'
			within('div#content div.article div.intro') { page.should have_content('Welcome to Peters Petshop') }
		end

		it "should show the navigation with all visible pages" do
			pages = [
				Factory(:public_page, :language => @default_language, :visible => true, :name => 'Page 1', :parent_id => @default_language_root.id),
				Factory(:public_page, :language => @default_language, :visible => true, :name => 'Page 2', :parent_id => @default_language_root.id)
			]
			visit '/alchemy/'
			within('div#navigation ul') { page.should have_selector('li a[href="/alchemy/page-1"], li a[href="/alchemy/page-2"]') }
		end

	end

	describe "fulltext search" do

		before(:each) do
			@page = Factory(:public_page, :language => @default_language, :visible => true, :name => 'Page 1', :parent_id => @default_language_root.id)
			@element = Factory(:element, :name => 'article', :page => @page)
			Factory(:public_page, :language => @default_language, :name => 'Suche', :page_layout => 'search', :parent_id => @default_language_root.id)
		end

		it "should have a correct path in the form tag" do
			visit('/alchemy/suche')
			page.should have_selector('div#content form[action="/alchemy/suche"]')
		end

		context "performing the search" do

			it "should display search results for richtext essences" do
				@element.content_by_name('text').essence.update_attributes(:body => '<p>Welcome to Peters Petshop</p>', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content .search_result') { page.should have_content('Petshop') }
			end

			it "should display search results for text essences" do
				@element.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content .search_result') { page.should have_content('Petshop') }
			end

			it "should not find contents placed on global-pages (layoutpage => true)" do
				@page.update_attributes(:layoutpage => true)
				@element.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content') { page.should have_css('h2.no_search_results') }
			end

			it "should not find contents placed on unpublished pages (public => false)" do
				@page.update_attributes(:public => false)
				@element.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content') { page.should have_css('h2.no_search_results') }
			end

			it "should not find contents placed on restricted pages (restricted => true)" do
				@page.update_attributes(:restricted => true)
				@element.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content') { page.should have_css('h2.no_search_results') }
			end

		end

	end

	describe "redirecting" do

		context "in multi language mode", :focus => true do

			before(:each) do
				@page = Factory(:public_page)
				Alchemy::Config.stub!(:get) { |arg| arg == :url_nesting ? true : Alchemy::Config.parameter(arg) }
			end

			it "should redirect to url with nested language code" do
				visit '/alchemy/a-public-page'
				page.current_path.should == '/alchemy/de/a-public-page'
			end

			context "should redirect to public child" do

				before(:each) do
					@page.update_attributes(:public => false, :name => 'Not Public', :urlname => '')
					@child = Factory(:public_page, :name => 'Public Child', :parent_id => @page.id)
					Alchemy::Config.stub!(:get) { |arg| arg == :url_nesting ? false : Alchemy::Config.parameter(arg) }
				end

				it "if requested page is unpublished" do
					visit '/alchemy/kl/not-public'
					page.current_path.should == '/alchemy/kl/public-child'
				end

				it "with nested language code, if requested page is unpublished and url has no language code" do
					visit '/alchemy/not-public'<commit_msg>Refactoring sub_navigation renderer and adding tests for it.<commit_after>require 'spec_helper'

describe Alchemy::PagesController do
	
	before(:each) do
		@default_language = Alchemy::Language.get_default
		@default_language_root = Factory(:language_root_page, :language => @default_language, :name => 'Home')
	end

	describe "#show" do

		it "should include all its elements and contents" do
			p = Factory(:public_page, :language => @default_language)
			article = p.elements.find_by_name('article')
			article.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
			visit '/alchemy/a-public-page'
			within('div#content div.article div.intro') { page.should have_content('Welcome to Peters Petshop') }
		end

		it "should show the navigation with all visible pages" do
			pages = [
				Factory(:public_page, :language => @default_language, :visible => true, :name => 'Page 1', :parent_id => @default_language_root.id),
				Factory(:public_page, :language => @default_language, :visible => true, :name => 'Page 2', :parent_id => @default_language_root.id)
			]
			visit '/alchemy/'
			within('div#navigation ul') { page.should have_selector('li a[href="/alchemy/page-1"], li a[href="/alchemy/page-2"]') }
		end

	end

	describe "fulltext search" do

		before(:each) do
			@page = Factory(:public_page, :language => @default_language, :visible => true, :name => 'Page 1', :parent_id => @default_language_root.id)
			@element = Factory(:element, :name => 'article', :page => @page)
			Factory(:public_page, :language => @default_language, :name => 'Suche', :page_layout => 'search', :parent_id => @default_language_root.id)
		end

		it "should have a correct path in the form tag" do
			visit('/alchemy/suche')
			page.should have_selector('div#content form[action="/alchemy/suche"]')
		end

		context "performing the search" do

			it "should display search results for richtext essences" do
				@element.content_by_name('text').essence.update_attributes(:body => '<p>Welcome to Peters Petshop</p>', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content .search_result') { page.should have_content('Petshop') }
			end

			it "should display search results for text essences" do
				@element.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content .search_result') { page.should have_content('Petshop') }
			end

			it "should not find contents placed on global-pages (layoutpage => true)" do
				@page.update_attributes(:layoutpage => true)
				@element.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content') { page.should have_css('h2.no_search_results') }
			end

			it "should not find contents placed on unpublished pages (public => false)" do
				@page.update_attributes(:public => false)
				@element.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content') { page.should have_css('h2.no_search_results') }
			end

			it "should not find contents placed on restricted pages (restricted => true)" do
				@page.update_attributes(:restricted => true)
				@element.content_by_name('intro').essence.update_attributes(:body => 'Welcome to Peters Petshop', :public => true)
				visit('/alchemy/suche?query=Petshop')
				within('div#content') { page.should have_css('h2.no_search_results') }
			end

		end

	end

	describe "redirecting" do

		context "in multi language mode" do

			before(:each) do
				@page = Factory(:public_page)
				Alchemy::Config.stub!(:get) { |arg| arg == :url_nesting ? true : Alchemy::Config.parameter(arg) }
			end

			it "should redirect to url with nested language code" do
				visit '/alchemy/a-public-page'
				page.current_path.should == '/alchemy/de/a-public-page'
			end

			context "should redirect to public child" do

				before(:each) do
					@page.update_attributes(:public => false, :name => 'Not Public', :urlname => '')
					@child = Factory(:public_page, :name => 'Public Child', :parent_id => @page.id)
					Alchemy::Config.stub!(:get) { |arg| arg == :url_nesting ? false : Alchemy::Config.parameter(arg) }
				end

				it "if requested page is unpublished" do
					visit '/alchemy/kl/not-public'
					page.current_path.should == '/alchemy/kl/public-child'
				end

				it "with nested language code, if requested page is unpublished and url has no language code" do
					visit '/alchemy/not-public'
{'fraction_non_alphanumeric': 0.1425201072386059, 'fraction_numerical': 0.001715817694369973, 'mean_word_length': 4.511820330969267, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 96, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3421269', 'n_tokens_mistral': 3353, 'n_tokens_neox': 2830, 'n_words': 740}