From 2dd9b8a38ad545c98355115589a9060b93de0b03 Mon Sep 17 00:00:00 2001
From: Dongqing Hu <sorra@outlook.com>
Date: Mon, 20 Mar 2017 13:53:23 +0000
Subject: [PATCH] Fix Project Wiki update

---
 app/controllers/projects/wikis_controller.rb  |  3 +-
 app/helpers/nav_helper.rb                     |  1 +
 app/models/wiki_page.rb                       |  8 +++--
 .../29405-fix-project-wiki-update.yml         |  4 +++
 .../wiki/user_updates_wiki_page_spec.rb       | 33 ++++++++++++++-----
 5 files changed, 36 insertions(+), 13 deletions(-)
 create mode 100644 changelogs/unreleased/29405-fix-project-wiki-update.yml

diff --git a/app/controllers/projects/wikis_controller.rb b/app/controllers/projects/wikis_controller.rb
index 8b6c83d4fed..f210f7e61d2 100644
--- a/app/controllers/projects/wikis_controller.rb
+++ b/app/controllers/projects/wikis_controller.rb
@@ -45,8 +45,9 @@ class Projects::WikisController < Projects::ApplicationController
     return render('empty') unless can?(current_user, :create_wiki, @project)
 
     @page = @project_wiki.find_page(params[:id])
+    @page = WikiPages::UpdateService.new(@project, current_user, wiki_params).execute(@page)
 
-    if @page = WikiPages::UpdateService.new(@project, current_user, wiki_params).execute(@page)
+    if @page.valid?
       redirect_to(
         namespace_project_wiki_path(@project.namespace, @project, @page),
         notice: 'Wiki was successfully updated.'
diff --git a/app/helpers/nav_helper.rb b/app/helpers/nav_helper.rb
index c1523b4dabf..a8f167cbff2 100644
--- a/app/helpers/nav_helper.rb
+++ b/app/helpers/nav_helper.rb
@@ -16,6 +16,7 @@ module NavHelper
       "page-gutter build-sidebar right-sidebar-expanded"
     elsif current_path?('wikis#show') ||
         current_path?('wikis#edit') ||
+        current_path?('wikis#update') ||
         current_path?('wikis#history') ||
         current_path?('wikis#git_access')
       "page-gutter wiki-sidebar right-sidebar-expanded"
diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb
index 465c4d903ac..c771c22f46a 100644
--- a/app/models/wiki_page.rb
+++ b/app/models/wiki_page.rb
@@ -155,7 +155,7 @@ class WikiPage
   end
 
   # Returns boolean True or False if this instance
-  # has been fully saved to disk or not.
+  # has been fully created on disk or not.
   def persisted?
     @persisted == true
   end
@@ -226,6 +226,8 @@ class WikiPage
   end
 
   def save(method, *args)
+    saved = false
+
     project_wiki = wiki
     if valid? && project_wiki.send(method, *args)
 
@@ -243,10 +245,10 @@ class WikiPage
       set_attributes
 
       @persisted = true
+      saved = true
     else
       errors.add(:base, project_wiki.error_message) if project_wiki.error_message
-      @persisted = false
     end
-    @persisted
+    saved
   end
 end
diff --git a/changelogs/unreleased/29405-fix-project-wiki-update.yml b/changelogs/unreleased/29405-fix-project-wiki-update.yml
new file mode 100644
index 00000000000..85be36f7902
--- /dev/null
+++ b/changelogs/unreleased/29405-fix-project-wiki-update.yml
@@ -0,0 +1,4 @@
+---
+title: Fix Project Wiki update
+merge_request: 9990
+author: Dongqing Hu
diff --git a/spec/features/projects/wiki/user_updates_wiki_page_spec.rb b/spec/features/projects/wiki/user_updates_wiki_page_spec.rb
index f842d14fa96..aedc0333cb9 100644
--- a/spec/features/projects/wiki/user_updates_wiki_page_spec.rb
+++ b/spec/features/projects/wiki/user_updates_wiki_page_spec.rb
@@ -15,15 +15,30 @@ feature 'Projects > Wiki > User updates wiki page', feature: true do
   context 'in the user namespace' do
     let(:project) { create(:project, namespace: user.namespace) }
 
-    scenario 'the home page' do
-      click_link 'Edit'
-
-      fill_in :wiki_content, with: 'My awesome wiki!'
-      click_button 'Save changes'
-
-      expect(page).to have_content('Home')
-      expect(page).to have_content("Last edited by #{user.name}")
-      expect(page).to have_content('My awesome wiki!')
+    context 'the home page' do
+      scenario 'success when the wiki content is not empty' do
+        click_link 'Edit'
+
+        fill_in :wiki_content, with: 'My awesome wiki!'
+        click_button 'Save changes'
+
+        expect(page).to have_content('Home')
+        expect(page).to have_content("Last edited by #{user.name}")
+        expect(page).to have_content('My awesome wiki!')
+      end
+
+      scenario 'failure when the wiki content is empty' do
+        click_link 'Edit'
+
+        fill_in :wiki_content, with: ''
+        click_button 'Save changes'
+
+        expect(page).to have_selector('.wiki-form')
+        expect(page).to have_content('Edit Page')
+        expect(page).to have_content('The form contains the following error:')
+        expect(page).to have_content('Content can\'t be blank')
+        expect(find('textarea#wiki_content').value).to eq ''
+      end
     end
   end
 
-- 
GitLab