mirror of
				https://github.com/getgrav/grav-plugin-admin.git
				synced 2025-10-31 18:35:57 +01:00 
			
		
		
		
	Fix #821 issue in saving a page to a new language when the filename does not contain the filename yet.
Also extract determineFilenameIncludingLanguage() method and add corresponding test.
This commit is contained in:
		| @@ -2361,21 +2361,9 @@ class AdminController | |||||||
|  |  | ||||||
|         $file = $obj->file(); |         $file = $obj->file(); | ||||||
|         if ($file) { |         if ($file) { | ||||||
|             $filename = substr($obj->name(), 0, -(strlen('.' . $language . '.md'))); |             $filename = $this->determineFilenameIncludingLanguage($obj->name(), $language); | ||||||
|  |  | ||||||
|             if (substr($filename, -3, 1) == '.') { |             $path = $obj->path() . DS . $filename; | ||||||
|                 if (substr($filename, -2) == substr($language, 0, 2)) { |  | ||||||
|                     $filename = str_replace(substr($filename, -2), $language, $filename); |  | ||||||
|                 } |  | ||||||
|             } elseif (substr($filename, -6, 1) == '.') { |  | ||||||
|                 if (substr($filename, -5) == substr($language, 0, 5)) { |  | ||||||
|                     $filename = str_replace(substr($filename, -5), $language, $filename); |  | ||||||
|                 } |  | ||||||
|             } else { |  | ||||||
|                 $filename .= '.' . $language; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             $path = $obj->path() . DS . $filename . '.md'; |  | ||||||
|             $aFile = File::instance($path); |             $aFile = File::instance($path); | ||||||
|             $aFile->save(); |             $aFile->save(); | ||||||
|  |  | ||||||
| @@ -2394,6 +2382,29 @@ class AdminController | |||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * The what should be the new filename when saving as a new language | ||||||
|  |      * | ||||||
|  |      * @param string $current_filename the current file name, including .md. Example: default.en.md | ||||||
|  |      * @param string $language The new language it will be saved as. Example: 'it' or 'en-GB'. | ||||||
|  |      * | ||||||
|  |      * @return string The new filename. Example: 'default.it' | ||||||
|  |      */ | ||||||
|  |     public function determineFilenameIncludingLanguage($current_filename, $language) | ||||||
|  |     { | ||||||
|  |         $filename = substr($current_filename, 0, -(strlen('.md'))); | ||||||
|  |  | ||||||
|  |         if (substr($filename, -3, 1) == '.') { | ||||||
|  |             $filename = str_replace(substr($filename, -2), $language, $filename); | ||||||
|  |         } elseif (substr($filename, -6, 1) == '.') { | ||||||
|  |             $filename = str_replace(substr($filename, -5), $language, $filename); | ||||||
|  |         } else { | ||||||
|  |             $filename .= '.' . $language; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return $filename . '.md'; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Determine if the user can edit media |      * Determine if the user can edit media | ||||||
|      * |      * | ||||||
|   | |||||||
							
								
								
									
										41
									
								
								tests/unit/classes/controllerTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								tests/unit/classes/controllerTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | namespace Grav\Plugin; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Class ControllerTest | ||||||
|  |  */ | ||||||
|  | class ControllerTest extends \Codeception\TestCase\Test | ||||||
|  | { | ||||||
|  |     protected $controller; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     protected function _before() | ||||||
|  |     { | ||||||
|  |         require_once(__DIR__ . '/../../../classes/controller.php'); | ||||||
|  |         $this->controller = new AdminController(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     protected function _after() | ||||||
|  |     { | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public function testDetermineFilenameIncludingLanguage() | ||||||
|  |     { | ||||||
|  |         $language = 'en-GB'; | ||||||
|  |  | ||||||
|  |         $this->assertSame('testing.en-GB.md', $this->controller->determineFilenameIncludingLanguage('testing.md', $language)); | ||||||
|  |         $this->assertSame('testing.en-GB.md', $this->controller->determineFilenameIncludingLanguage('testing.en.md', $language)); | ||||||
|  |         $this->assertSame('testing.en-GB.md', $this->controller->determineFilenameIncludingLanguage('testing.it.md', $language)); | ||||||
|  |         $this->assertSame('testing.en-GB.md', $this->controller->determineFilenameIncludingLanguage('testing.en-GB.md', $language)); | ||||||
|  |  | ||||||
|  |         $language = 'it'; | ||||||
|  |  | ||||||
|  |         $this->assertSame('testing.it.md', $this->controller->determineFilenameIncludingLanguage('testing.md', $language)); | ||||||
|  |         $this->assertSame('testing.it.md', $this->controller->determineFilenameIncludingLanguage('testing.en.md', $language)); | ||||||
|  |         $this->assertSame('testing.it.md', $this->controller->determineFilenameIncludingLanguage('testing.it.md', $language)); | ||||||
|  |         $this->assertSame('testing.it.md', $this->controller->determineFilenameIncludingLanguage('testing.en-GB.md', $language)); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
		Reference in New Issue
	
	Block a user