commit 8387eb79d86947a63b3856c5ce64936f39c2a9a0
Author: Shlomi Zadok <shlomi@ben-hanna.com>
Date:   Sat Sep 12 18:35:59 2015 +0300

    fixes #11037 - Add ByIdName to environment model

diff --git a/app/models/environment.rb b/app/models/environment.rb
index 47cbebc92..8ee3d7490 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -1,9 +1,7 @@
 class Environment < ActiveRecord::Base
-  extend FriendlyId
-  friendly_id :name
   include Taxonomix
   include Authorizable
-  include Parameterizable::ByName
+  include Parameterizable::ByIdName
 
   validates_lengths_from_database
   before_destroy EnsureNotUsedBy.new(:hosts, :hostgroups)
diff --git a/test/functional/api/v2/smart_proxies_controller_test.rb b/test/functional/api/v2/smart_proxies_controller_test.rb
index 0a48d1693..203396041 100644
--- a/test/functional/api/v2/smart_proxies_controller_test.rb
+++ b/test/functional/api/v2/smart_proxies_controller_test.rb
@@ -181,7 +181,7 @@ class Api::V2::SmartProxiesControllerTest < ActionController::TestCase
   test "should obsolete puppetclasses" do
     setup_import_classes
     as_admin do
-      assert_difference('Environment.find("env1").puppetclasses.count', -2) do
+      assert_difference('Environment.find_by_name("env1").puppetclasses.count', -2) do
         post :import_puppetclasses, {:id => smart_proxies(:puppetmaster).id}, set_session_user
       end
     end
diff --git a/test/functional/environments_controller_test.rb b/test/functional/environments_controller_test.rb
index 6e305a6c5..a2e99243c 100644
--- a/test/functional/environments_controller_test.rb
+++ b/test/functional/environments_controller_test.rb
@@ -24,7 +24,7 @@ class EnvironmentsControllerTest < ActionController::TestCase
     environment = Environment.new :name => "some_environment"
     assert environment.save!
 
-    get :edit, {:id => environment.name}, set_session_user
+    get :edit, {:id => environment.to_param}, set_session_user
     assert_response :success
   end
 
@@ -33,7 +33,7 @@ class EnvironmentsControllerTest < ActionController::TestCase
     environment = Environment.new :name => "some_environment"
     assert environment.save!
 
-    put :update, { :commit => "Update", :id => environment.name, :environment => {:name => "other_environment"} }, set_session_user
+    put :update, { :commit => "Update", :id => environment.to_param, :environment => {:name => "other_environment"} }, set_session_user
     env = Environment.find(environment)
     assert env.name == "other_environment"
 
@@ -46,7 +46,7 @@ class EnvironmentsControllerTest < ActionController::TestCase
     assert environment.save!
 
     assert_difference('Environment.count', -1) do
-      delete :destroy, {:id => environment.name}, set_session_user
+      delete :destroy, {:id => environment.to_param}, set_session_user
     end
 
     assert_redirected_to environments_path
@@ -97,6 +97,7 @@ class EnvironmentsControllerTest < ActionController::TestCase
     assert_equal "Successfully updated environments and Puppet classes from the on-disk Puppet installation", flash[:notice]
     assert Environment.find_by_name("env1").puppetclasses.map(&:name).sort == ["a", "b", "c"]
   end
+
   test "should handle disk environment containing less classes" do
     setup_import_classes
     as_admin {Puppetclass.create(:name => "d")}
@@ -192,4 +193,17 @@ class EnvironmentsControllerTest < ActionController::TestCase
     get :index, {}, set_session_user
     assert_response :success
   end
+
+  test "should accept environment with name 'name'" do
+    @request.env["HTTP_REFERER"] = environments_url
+    ProxyAPI::Puppet.any_instance.stubs(:environments).returns(["new"])
+    get :import_environments, {:proxy => smart_proxies(:puppetmaster)}, set_session_user
+    post :obsolete_and_new,
+         {"changed" =>
+              {"new" =>
+                   {"new" => '{"a":{"new":{}}}'}
+              }
+         }, set_session_user
+    assert(Environment.all.map(&:name).include?('new'), 'Should include environment with name "new"')
+  end
 end
diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb
index 62fe96c76..dae8d7c53 100644
--- a/test/unit/environment_test.rb
+++ b/test/unit/environment_test.rb
@@ -10,14 +10,14 @@ class EnvironmentTest < ActiveSupport::TestCase
 
   test "should have name" do
     env = Environment.new
-    assert !env.valid?
+    refute env.valid?
   end
 
   test "name should be unique" do
     as_admin do
       env = Environment.create :name => "foo"
       env2 = Environment.new :name => env.name
-      assert !env2.valid?
+      refute env2.valid?
     end
   end
 
@@ -46,4 +46,9 @@ class EnvironmentTest < ActiveSupport::TestCase
       environment.reload
     end
   end
+
+  test "Should create environment with the name 'new'" do
+    environment = Environment.new(:name => 'new')
+    assert environment.valid?
+  end
 end
