Clone
page_repository_spec.rb
require 'spec_helper'

require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb'
require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb'

describe Locomotive::Steam::PageRepository do

  shared_examples_for 'a repository' do

    let(:site)        { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) }
    let(:locale)      { :en }
    let(:repository)  { described_class.new(adapter, site, locale) }

    describe '#all' do
      let(:conditions) { {} }
      subject { repository.all(conditions) }
      it { expect(subject.size).to eq 26 }

      context 'with conditions' do
        let(:conditions) { { fullpath: 'index', 'slug.ne' => '404' } }
        it { expect(subject.size).to eq 1 }
      end
    end

    describe '#query' do
      subject { repository.query { where(fullpath: 'index') }.first }
      it { expect(subject.title[:en]).to eq 'Home page' }
    end

    describe '#by_handle' do
      subject { repository.by_handle('our-music') }
      it { expect(subject.title[:en]).to eq 'Music' }
    end

    describe '#only_handle_and_fullpath' do
      subject { repository.only_handle_and_fullpath }
      it { expect(subject.size).to eq 3 }
    end

    describe '#by_fullpath' do
      subject { repository.by_fullpath('archives/news') }
      it { expect(subject.title[:en]).to eq 'News archive' }
    end

    describe '#template_for' do
      let(:type_repository)   { Locomotive::Steam::ContentTypeRepository.new(adapter, site, locale) }
      let(:type)              { type_repository.by_slug('songs') }
      let(:entry_repository)  { Locomotive::Steam::ContentEntryRepository.new(adapter, site, locale, type_repository).with(type) }
      let(:entry)             { entry_repository.by_slug('song-number-1') }
      subject { repository.template_for(entry) }
      it { expect(subject.title[:en]).to eq 'A song template' }
    end

    describe '#matching_fullpath' do
      subject { repository.matching_fullpath(['songs/content_type_template', 'content_type_template/songs', 'songs/song-number-1']) }
      it { expect(subject.size).to eq 2 }
    end

    describe '#root' do
      subject { repository.root }
      it { expect(subject.title[:en]).to eq 'Home page' }
      it { expect(subject.title[:fr]).to eq "Page d'accueil" }
    end

    describe '#parent_of' do
      let(:page) { repository.by_handle('about-us') }
      subject { repository.parent_of(page) }
      it { expect(subject.title[:en]).to eq 'Home page' }
    end

    describe '#ancestors_of' do
      let(:page) { repository.by_handle('about-us') }
      subject { repository.ancestors_of(page) }
      it { expect(subject.size).to eq 2 }
      it { expect(subject.first.title[:en]).to eq 'Home page' }
    end

    describe '#children_of' do
      let(:page) { repository.root }
      subject { repository.children_of(page) }
      it { expect(subject.size).to eq 15 }
    end

    describe '#editable_element_for' do
      let(:page) { repository.by_handle('about-us') }
      subject { repository.editable_element_for(page, 'banner', 'pitch') }
      it { expect(subject.content[:en]).to eq '<h2>About us</h2><p>Lorem ipsum...</p>' }
    end

  end

  context 'MongoDB' do

    it_should_behave_like 'a repository' do

      let(:site_id)       { mongodb_site_id }
      let(:adapter)       { Locomotive::Steam::MongoDBAdapter.new(database: 'steam_test', hosts: ['127.0.0.1:27017']) }

    end

  end

  context 'Filesystem' do

    it_should_behave_like 'a repository' do

      let(:site_id) { 1 }
      let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) }

      after(:all) { Locomotive::Steam::Adapters::Filesystem::SimpleCacheStore.new.clear }

    end

  end

end