]> BookStack Code Mirror - bookstack/commitdiff
Pages: Updated editor field to always be set
authorDan Brown <redacted>
Sun, 29 Sep 2024 13:36:41 +0000 (14:36 +0100)
committerDan Brown <redacted>
Sun, 29 Sep 2024 13:36:41 +0000 (14:36 +0100)
- Migration for setting on existing pages
- Added test to cover simple new page scenario

For #5117

app/Entities/Repos/PageRepo.php
database/migrations/2024_09_29_140340_ensure_editor_value_set.php [new file with mode: 0644]
tests/Entity/PageEditorTest.php

index ce7e34ae1ec90f7e35600aa7fb8ca2b8aecb93bf..1bc15392cec7b4478372b761c4179e3fa364f297 100644 (file)
@@ -11,7 +11,6 @@ use BookStack\Entities\Models\PageRevision;
 use BookStack\Entities\Queries\EntityQueries;
 use BookStack\Entities\Tools\BookContents;
 use BookStack\Entities\Tools\PageContent;
-use BookStack\Entities\Tools\PageEditorData;
 use BookStack\Entities\Tools\PageEditorType;
 use BookStack\Entities\Tools\TrashCan;
 use BookStack\Exceptions\MoveOperationException;
@@ -44,6 +43,7 @@ class PageRepo
             'owned_by'   => user()->id,
             'updated_by' => user()->id,
             'draft'      => true,
+            'editor'     => PageEditorType::getSystemDefault()->value,
         ]);
 
         if ($parent instanceof Chapter) {
@@ -146,8 +146,10 @@ class PageRepo
             $pageContent->setNewHTML($input['html'], user());
         }
 
-        if ($newEditor !== $currentEditor && userCan('editor-change')) {
+        if (($newEditor !== $currentEditor || empty($page->editor)) && userCan('editor-change')) {
             $page->editor = $newEditor->value;
+        } elseif (empty($page->editor)) {
+            $page->editor = $defaultEditor->value;
         }
     }
 
diff --git a/database/migrations/2024_09_29_140340_ensure_editor_value_set.php b/database/migrations/2024_09_29_140340_ensure_editor_value_set.php
new file mode 100644 (file)
index 0000000..786322b
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Support\Facades\DB;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        // Ensure we have an "editor" value set for pages
+
+        // Get default
+        $default = DB::table('settings')
+            ->where('setting_key', '=', 'app-editor')
+            ->first()
+            ->value ?? 'wysiwyg';
+        $default = ($default === 'markdown') ? 'markdown' : 'wysiwyg';
+
+        // We set it to 'markdown' for pages currently with markdown content
+        DB::table('pages')
+            ->where('editor', '=', '')
+            ->where('markdown', '!=', '')
+            ->update(['editor' => 'markdown']);
+
+        // We set it to 'wysiwyg' where we have HTML but no markdown
+        DB::table('pages')
+            ->where('editor', '=', '')
+            ->where('markdown', '=', '')
+            ->where('html', '!=', '')
+            ->update(['editor' => 'wysiwyg']);
+
+        // Otherwise, where still empty, set to the current default
+        DB::table('pages')
+            ->where('editor', '=', '')
+            ->update(['editor' => $default]);
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        // Can't reverse due to not knowing what would have been empty before
+    }
+};
index 9340249568c0d58718d325d183365cdc3b5905a4..21abd4ba1e6fb54c36b5aab96b061aeed60a45b1 100644 (file)
@@ -24,6 +24,21 @@ class PageEditorTest extends TestCase
         $this->withHtml($this->followRedirects($resp))->assertElementExists('#html-editor');
     }
 
+    public function test_editor_set_for_new_pages()
+    {
+        $book = $this->page->book;
+
+        $this->asEditor()->get($book->getUrl('/create-page'));
+        $newPage = $book->pages()->orderBy('id', 'desc')->first();
+        $this->assertEquals('wysiwyg', $newPage->editor);
+
+        $this->setSettings(['app-editor' => PageEditorType::Markdown->value]);
+
+        $this->asEditor()->get($book->getUrl('/create-page'));
+        $newPage = $book->pages()->orderBy('id', 'desc')->first();
+        $this->assertEquals('markdown', $newPage->editor);
+    }
+
     public function test_markdown_setting_shows_markdown_editor_for_new_pages()
     {
         $this->setSettings(['app-editor' => PageEditorType::Markdown->value]);
Morty Proxy This is a proxified and sanitized view of the page, visit original site.