require 'spec_helper' describe Locomotive::Export do context '#content_type' do before(:each) do site = FactoryGirl.build('another site') Locomotive::Site.stubs(:find).returns(site) project_type = build_project_type(site) project_type.entries.build(:title => 'Project #1', :description => 'Lorem ipsum', :active => true) project_type.entries.build(:title => 'Project #2', :description => 'More Lorem ipsum', :active => false) team_type = build_team_type(site, project_type) team_type.entries.build(:name => 'Ben', :projects => project_type.entries, :current_project => project_type.entries.first) team_type.entries.build(:name => 'Zach', :current_project => project_type.entries.last) @project_data = ::Locomotive::Export.new(site).send(:extract_contents, project_type) @team_data = ::Locomotive::Export.new(site).send(:extract_contents, team_type) end it 'includes the exact number of entries' do @project_data.size.should == 2 @project_data.collect { |n| n.keys.first }.should == ['Project #1', 'Project #2'] end it 'deals with real booleans' do @project_data.first.values.first['active'].should === true @project_data.last.values.first['active'].should === false end it 'stores the list of highlighted values in a has_many relationship' do @team_data.first.values.first['projects'].size.should == 2 @team_data.first.values.first['projects'].should == ['Project #1', 'Project #2'] @team_data.last.values.first['projects'].should be_nil end it 'stores a highlighted value in a has_one relationship' do @team_data.collect { |n| n.values.first['current_project'] }.should == ['Project #1', 'Project #2'] end def build_project_type(site) FactoryGirl.build(:content_type, :site => site, :highlighted_field_name => 'custom_field_1').tap do |content_type| content_type.entries_custom_fields.build :label => 'Title', :name => 'title', :type => 'string' content_type.entries_custom_fields.build :label => 'My Description', :name => 'description', :type => 'text' content_type.entries_custom_fields.build :label => 'Active', :type => 'boolean' end end def build_team_type(site, project_type) Object.send(:remove_const, 'TestProject') rescue nil klass = Object.const_set('TestProject', Class.new { def self.embedded?; false; end }) content_type = FactoryGirl.build(:content_type, :site => site, :name => 'team', :highlighted_field_name => 'custom_field_1') content_type.entries_custom_fields.build :label => 'Name', :name => 'name', :type => 'string' content_type.entries_custom_fields.build :label => 'Projects', :type => 'has_many', :name => 'projects', :target => 'TestProject' content_type.entries_custom_fields.build :label => 'Bio', :name => 'bio', :type => 'text' content_type.entries_custom_fields.build :label => 'Current Project', :type => 'has_one', :name => 'current_project', :target => 'TestProject' content_type end end context '#zipfile' do before(:all) do @site = FactoryGirl.create('another site') # first import a brand new site self.import_it # then export it of course @zip_file = self.export_it end it 'generates a zipfile' do @zip_file.should_not be_nil File.exists?(@zip_file).should be_true end it 'has the exact number of pages from the original site' do self.unzip Dir[File.join(self.zip_folder, 'app', 'views', 'pages', '**/*.liquid')].size.should == 11 end it 'includes snippets' do self.unzip Dir[File.join(self.zip_folder, 'app', 'views', 'snippets', '*.liquid')].size.should == 1 end it 'has yaml files describing the content types as well as their data' do self.unzip Dir[File.join(self.zip_folder, 'app', 'content_types', '*.yml')].size.should == 4 Dir[File.join(self.zip_folder, 'data', '*.yml')].size.should == 4 end def import_it job = Locomotive::Import::Job.new(FixturedTheme.duplicate_and_open('default.zip'), @site, { :samples => true, :reset => true }) job.perform job.success nil end def export_it ::Locomotive::Export.run!(@site, @site.name.parameterize) end def zip_folder File.join(File.dirname(@zip_file), 'locomotive-test-website-2') end def unzip return if @zip_file || File.exists?(self.zip_folder) Zip::ZipFile.open(@zip_file) do |zipfile| destination_path = File.dirname(@zip_file) zipfile.each do |entry| FileUtils.mkdir_p(File.dirname(File.join(destination_path, entry.name))) zipfile.extract(entry, File.join(destination_path, entry.name)) end end end after(:all) do FileUtils.rm_rf(self.zip_folder) if File.exists?(self.zip_folder) Locomotive::Site.destroy_all end end end