
require 'spec_helper'
Capybara.add_selector(:href) do
  xpath {|href| XPath.descendant[XPath.attr(:href).contains(href)] }
end

feature 'Administration', :type=> :feature do 
  let!(:user) {FactoryGirl.create(:user)}
  let!(:rubrik) {FactoryGirl.create(:rubrik)}


  let!(:themengruppe){    FactoryGirl.create(:themengruppe) }
  let!(:studium) {FactoryGirl.create(:studium,:visible=>true)}
  before(:each) do 
    FactoryGirl.create(:neuigkeit, rubrik_id: rubrik.id, author_id: user.id)
    FactoryGirl.create(:thema, id: t=YAML.load_file("#{::Rails.root.to_s}/config/start_topic.yml").to_i, themengruppe_id: themengruppe.id) 
    studium.id
  end
  def sign_in  
    visit '/'
    user.add_role(:fetadmin)

    find(:href, "sign_in").click
    fill_in 'user_email', with: user.email
    fill_in 'user_password', with:"password"
    click_button 'Sign in'
  end 

  scenario 'change preferred theme' do
    sign_in
    visit "/admin/users/#{user.id}/set_preferred_theme?theme=blue2"
    expect(User.find(user.id).preferredtheme).to eq("blue2")
    visit "/admin/users/#{user.id}/set_preferred_theme?theme=blue1"
    expect(User.find(user.id).preferredtheme).to eq("blue1")
  end
end
