Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate zip file names with the version number in them #143

Merged
merged 5 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions inc/ZipProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,32 @@ class ZipProvider {
protected const BUILD_TIME_KEY = '_traduttore_build_time';

/**
* The GlotPress translation set.
* The current GlotPress translation set.
*
* @since 2.0.0
*
* @var GP_Translation_Set The translation set.
*/
protected $translation_set;

/**
* The current GlotPress locale.
*
* @since 3.0.0
*
* @var GP_Locale The locale.
*/
protected $locale;

/**
* The current project.
*
* @since 3.0.0
*
* @var Project The project.
*/
protected $project;

/**
* ZipProvider constructor.
*
Expand All @@ -60,6 +78,8 @@ class ZipProvider {
*/
public function __construct( GP_Translation_Set $translation_set ) {
$this->translation_set = $translation_set;
$this->locale = GP_Locales::by_slug( $this->translation_set->locale );
$this->project = new Project( GP::$project->get( $this->translation_set->project_id ) );
}

/**
Expand Down Expand Up @@ -161,8 +181,9 @@ public function generate_zip_file() : bool {
* @param string $file Path to the generated language pack.
* @param string $url URL to the generated language pack.
* @param GP_Translation_Set $translation_set Translation set the language pack is for.
* @param Project $project The translation set's project.
*/
do_action( 'traduttore.zip_generated', $this->get_zip_path(), $this->get_zip_url(), $this->translation_set );
do_action( 'traduttore.zip_generated', $this->get_zip_path(), $this->get_zip_url(), $this->translation_set, $this->project );

return true;
}
Expand Down Expand Up @@ -209,14 +230,22 @@ public function remove_zip_file() : bool {
* @return string ZIP filename.
*/
protected function get_zip_filename() : string {
/* @var GP_Locale $locale */
$locale = GP_Locales::by_slug( $this->translation_set->locale );
$project = GP::$project->get( $this->translation_set->project_id );
$slug = str_replace( '/', '-', $this->project->get_slug() );
$version = $this->project->get_version();

if ( $version ) {
return sprintf(
'%1$s-%2$s-%3$s.zip',
$slug,
$this->locale->wp_locale,
$version
);
}

return sprintf(
'%1$s-%2$s.zip',
str_replace( '/', '-', $project->slug ),
$locale->wp_locale
$slug,
$this->locale->wp_locale
);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/tests/ZipProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,28 @@ public function test_get_zip_path(): void {
$this->assertStringEndsWith( 'wp-content/traduttore/foo-project-de_DE.zip', $provider->get_zip_path() );
}

public function test_get_zip_path_with_version(): void {
$this->project->set_version( '2.0' );

$provider = new Provider( $this->translation_set );

$this->assertStringEndsWith( 'wp-content/traduttore/foo-project-de_DE-2.0.zip', $provider->get_zip_path() );
}

public function test_get_zip_url(): void {
$provider = new Provider( $this->translation_set );

$this->assertSame( home_url( 'wp-content/traduttore/foo-project-de_DE.zip' ), $provider->get_zip_url() );
}

public function test_get_zip_url_with_version(): void {
$this->project->set_version( '2.0' );

$provider = new Provider( $this->translation_set );

$this->assertSame( home_url( 'wp-content/traduttore/foo-project-de_DE-2.0.zip' ), $provider->get_zip_url() );
}

public function test_get_last_build_time_for_new_set(): void {
$provider = new Provider( $this->translation_set );

Expand Down