Skip to content

Commit

Permalink
Code Quality: Force import of functions / classes (#12760)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey committed Jan 19, 2023
1 parent 32490f8 commit 704525e
Show file tree
Hide file tree
Showing 43 changed files with 235 additions and 137 deletions.
7 changes: 5 additions & 2 deletions includes/templates/admin/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/

use Google\Web_Stories\Admin\Dashboard;
use Google\Web_Stories\Services;

/**
* Copyright 2020 Google LLC
*
Expand All @@ -29,7 +32,7 @@
die( '-1' );
}

$dashboard_settings = \Google\Web_Stories\Services::get( 'dashboard' )->get_dashboard_settings();
$dashboard_settings = Services::get( 'dashboard' )->get_dashboard_settings();

$init_script = <<<'JS'
wp.domReady( function() {
Expand All @@ -39,7 +42,7 @@

$script = sprintf( $init_script, wp_json_encode( $dashboard_settings ) );

wp_add_inline_script( \Google\Web_Stories\Admin\Dashboard::SCRIPT_HANDLE, $script );
wp_add_inline_script( Dashboard::SCRIPT_HANDLE, $script );

?>

Expand Down
10 changes: 7 additions & 3 deletions includes/templates/admin/edit-story.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/

use Google\Web_Stories\Admin\Editor;
use Google\Web_Stories\Services;
use function Google\Web_Stories\rest_preload_api_request;

/**
* Copyright 2020 Google LLC
*
Expand Down Expand Up @@ -134,7 +138,7 @@
$story_query_params['web_stories_demo'] = 'true';

$story_path = $story_initial_path . build_query( $story_query_params );
$story_data = \Google\Web_Stories\rest_preload_api_request( [], $story_path );
$story_data = rest_preload_api_request( [], $story_path );
$initial_edits['story'] = ! empty( $story_data[ $story_path ]['body'] ) ? $story_data[ $story_path ]['body'] : [];
}

Expand All @@ -161,7 +165,7 @@
require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';
register_and_do_post_meta_boxes( $post );

$editor_settings = \Google\Web_Stories\Services::get( 'editor' )->get_editor_settings();
$editor_settings = Services::get( 'editor' )->get_editor_settings();

wp_add_inline_script(
'wp-api-fetch',
Expand All @@ -177,7 +181,7 @@

$script = sprintf( $init_script, wp_json_encode( $editor_settings ), wp_json_encode( $initial_edits ) );

wp_add_inline_script( \Google\Web_Stories\Admin\Editor::SCRIPT_HANDLE, $script );
wp_add_inline_script( Editor::SCRIPT_HANDLE, $script );

require_once ABSPATH . 'wp-admin/admin-header.php';

Expand Down
7 changes: 5 additions & 2 deletions includes/templates/admin/experiments.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/

use Google\Web_Stories\Experiments;
use Google\Web_Stories\Settings;

/**
* Copyright 2020 Google LLC
*
Expand All @@ -34,8 +37,8 @@
<h1><?php esc_html_e( 'Experimental Settings', 'web-stories' ); ?></h1>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php settings_fields( \Google\Web_Stories\Settings::SETTING_GROUP_EXPERIMENTS ); ?>
<?php do_settings_sections( \Google\Web_Stories\Experiments::PAGE_NAME ); ?>
<?php settings_fields( Settings::SETTING_GROUP_EXPERIMENTS ); ?>
<?php do_settings_sections( Experiments::PAGE_NAME ); ?>
<?php submit_button(); ?>
</form>
</div>
Expand Down
12 changes: 11 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,19 @@
<property name="linesCountAfterLastUse" value="1" />
</properties>
</rule>
<!-- All references to functions, classes and constants should import using a use statement. -->
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>
<property name="allowFullyQualifiedGlobalFunctions" value="true" />
<property name="allowFullyQualifiedGlobalClasses" value="true" />
<property name="allowFullyQualifiedGlobalConstants" value="true" />
<property name="allowFallbackGlobalFunctions" value="true" />
<property name="allowFallbackGlobalConstants" value="true" />
<property name="allowFullyQualifiedNameForCollidingClasses" value="true" />
</properties>
</rule>
<!-- Prohibits uses from the same namespace. -->
<rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace" />
<!-- All references to types named Exception or ending with Exception must be referenced via a fully qualified name. -->
<!-- Looks for unused imports from other namespaces. -->
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<properties>
Expand Down
13 changes: 9 additions & 4 deletions tests/phpunit/integration/includes/Capabilities_Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@
namespace Google\Web_Stories\Tests\Integration;

use Google\Web_Stories\Infrastructure\Injector\SimpleInjector;
use Google\Web_Stories\Settings;
use Google\Web_Stories\Shopping\Shopping_Vendors;
use Google\Web_Stories\Story_Post_Type;
use Google\Web_Stories\User\Capabilities;

trait Capabilities_Setup {
public function add_caps_to_roles(): void {
$capability = new \Google\Web_Stories\User\Capabilities( new \Google\Web_Stories\Story_Post_Type( new \Google\Web_Stories\Settings( new Shopping_Vendors( new SimpleInjector() ) ) ) );
$capability->add_caps_to_roles();
$this->get_capabilities()->add_caps_to_roles();
}

public function remove_caps_from_roles(): void {
$capability = new \Google\Web_Stories\User\Capabilities( new \Google\Web_Stories\Story_Post_Type( new \Google\Web_Stories\Settings( new Shopping_Vendors( new SimpleInjector() ) ) ) );
$capability->remove_caps_from_roles();
$this->get_capabilities()->remove_caps_from_roles();
}

protected function get_capabilities(): Capabilities {
return new Capabilities( new Story_Post_Type( new Settings( new Shopping_Vendors( new SimpleInjector() ) ) ) );
}
}
9 changes: 6 additions & 3 deletions tests/phpunit/integration/includes/Kses_Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
namespace Google\Web_Stories\Tests\Integration;

use Google\Web_Stories\KSES;
use Google\Web_Stories\Page_Template_Post_Type;
use Google\Web_Stories\Settings;
use Google\Web_Stories\Story_Post_Type;

/**
* Trait Kses_Setup
Expand All @@ -33,11 +36,11 @@ trait Kses_Setup {
* Setup KSES init class.
*/
protected function kses_int(): void {
$settings = $this->createMock( \Google\Web_Stories\Settings::class );
$story_post_type = new \Google\Web_Stories\Story_Post_Type( $settings );
$settings = $this->createMock( Settings::class );
$story_post_type = new Story_Post_Type( $settings );
$this->kses = new KSES(
$story_post_type,
new \Google\Web_Stories\Page_Template_Post_Type( $story_post_type )
new Page_Template_Post_Type( $story_post_type )
);
$this->kses->register();
}
Expand Down
6 changes: 4 additions & 2 deletions tests/phpunit/integration/tests/AMP/Sanitization.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

use DOMElement;
use DOMNodeList;
use Google\Web_Stories\AMP\Meta_Sanitizer;
use Google\Web_Stories\AMP\Tag_And_Attribute_Sanitizer;
use Google\Web_Stories\Tests\Integration\DependencyInjectedTestCase;
use Google\Web_Stories_Dependencies\AMP_Dev_Mode_Sanitizer;
use Google\Web_Stories_Dependencies\AMP_Layout_Sanitizer;
Expand Down Expand Up @@ -491,8 +493,8 @@ static function( $sanitizers ) {
$this->assertEquals( 'Even_After_Validating_Sanitizer', $ordered_sanitizers[ \count( $ordered_sanitizers ) - 5 ] );
$this->assertEquals( AMP_Layout_Sanitizer::class, $ordered_sanitizers[ \count( $ordered_sanitizers ) - 4 ] );
$this->assertEquals( AMP_Style_Sanitizer::class, $ordered_sanitizers[ \count( $ordered_sanitizers ) - 3 ] );
$this->assertEquals( \Google\Web_Stories\AMP\Meta_Sanitizer::class, $ordered_sanitizers[ \count( $ordered_sanitizers ) - 2 ] );
$this->assertEquals( \Google\Web_Stories\AMP\Tag_And_Attribute_Sanitizer::class, $ordered_sanitizers[ \count( $ordered_sanitizers ) - 1 ] );
$this->assertEquals( Meta_Sanitizer::class, $ordered_sanitizers[ \count( $ordered_sanitizers ) - 2 ] );
$this->assertEquals( Tag_And_Attribute_Sanitizer::class, $ordered_sanitizers[ \count( $ordered_sanitizers ) - 1 ] );
}

/**
Expand Down
14 changes: 8 additions & 6 deletions tests/phpunit/integration/tests/AdSense.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace Google\Web_Stories\Tests\Integration;

use Google\Web_Stories\Settings;

/**
* @coversDefaultClass \Google\Web_Stories\AdSense
*/
Expand All @@ -29,19 +31,19 @@ class AdSense extends DependencyInjectedTestCase {
public function set_up(): void {
parent::set_up();

update_option( \Google\Web_Stories\Settings::SETTING_NAME_AD_NETWORK, 'adsense' );
update_option( \Google\Web_Stories\Settings::SETTING_NAME_ADSENSE_SLOT_ID, '123' );
update_option( \Google\Web_Stories\Settings::SETTING_NAME_ADSENSE_PUBLISHER_ID, '456' );
update_option( Settings::SETTING_NAME_AD_NETWORK, 'adsense' );
update_option( Settings::SETTING_NAME_ADSENSE_SLOT_ID, '123' );
update_option( Settings::SETTING_NAME_ADSENSE_PUBLISHER_ID, '456' );

$this->instance = $this->injector->make( \Google\Web_Stories\AdSense::class );
}

public function tear_down(): void {
parent::tear_down();

delete_option( \Google\Web_Stories\Settings::SETTING_NAME_AD_NETWORK );
delete_option( \Google\Web_Stories\Settings::SETTING_NAME_ADSENSE_SLOT_ID );
delete_option( \Google\Web_Stories\Settings::SETTING_NAME_ADSENSE_PUBLISHER_ID );
delete_option( Settings::SETTING_NAME_AD_NETWORK );
delete_option( Settings::SETTING_NAME_ADSENSE_SLOT_ID );
delete_option( Settings::SETTING_NAME_ADSENSE_PUBLISHER_ID );
}

/**
Expand Down
10 changes: 6 additions & 4 deletions tests/phpunit/integration/tests/Ad_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace Google\Web_Stories\Tests\Integration;

use Google\Web_Stories\Settings;

/**
* @coversDefaultClass \Google\Web_Stories\Ad_Manager
*/
Expand All @@ -29,17 +31,17 @@ class Ad_Manager extends DependencyInjectedTestCase {
public function set_up(): void {
parent::set_up();

update_option( \Google\Web_Stories\Settings::SETTING_NAME_AD_NETWORK, 'admanager' );
update_option( \Google\Web_Stories\Settings::SETTING_NAME_AD_MANAGER_SLOT_ID, '123' );
update_option( Settings::SETTING_NAME_AD_NETWORK, 'admanager' );
update_option( Settings::SETTING_NAME_AD_MANAGER_SLOT_ID, '123' );

$this->instance = $this->injector->make( \Google\Web_Stories\Ad_Manager::class );
}

public function tear_down(): void {
parent::tear_down();

delete_option( \Google\Web_Stories\Settings::SETTING_NAME_AD_NETWORK );
delete_option( \Google\Web_Stories\Settings::SETTING_NAME_AD_MANAGER_SLOT_ID );
delete_option( Settings::SETTING_NAME_AD_NETWORK );
delete_option( Settings::SETTING_NAME_AD_MANAGER_SLOT_ID );
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/phpunit/integration/tests/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace Google\Web_Stories\Tests\Integration\Admin;

use DateTime;
use Google\Web_Stories\Media\Image_Sizes;
use Google\Web_Stories\Settings;
use Google\Web_Stories\Story_Post_Type;
use Google\Web_Stories\Tests\Integration\DependencyInjectedTestCase;
Expand Down Expand Up @@ -127,7 +128,7 @@ public function test_prefill_post_content(): void {
$current_post = get_post( self::$post_id );
$this->assertNotNull( $current_post );
$result = $this->instance->prefill_post_content( 'current', $current_post );
$poster = (string) wp_get_attachment_image_url( (int) get_post_thumbnail_id( self::$story_id ), \Google\Web_Stories\Media\Image_Sizes::POSTER_PORTRAIT_IMAGE_SIZE );
$poster = (string) wp_get_attachment_image_url( (int) get_post_thumbnail_id( self::$story_id ), Image_Sizes::POSTER_PORTRAIT_IMAGE_SIZE );
$this->assertIsString( $result );
$this->assertStringContainsString( 'wp-block-web-stories-embed', $result );
$this->assertStringContainsString( $poster, $result );
Expand Down Expand Up @@ -187,7 +188,7 @@ public function test_prefill_post_content_shortcode(): void {
$current_post = get_post( self::$post_id );
$this->assertNotNull( $current_post );
$result = $this->instance->prefill_post_content( 'current', $current_post );
$poster = (string) wp_get_attachment_image_url( (int) get_post_thumbnail_id( self::$story_id ), \Google\Web_Stories\Media\Image_Sizes::POSTER_PORTRAIT_IMAGE_SIZE );
$poster = (string) wp_get_attachment_image_url( (int) get_post_thumbnail_id( self::$story_id ), Image_Sizes::POSTER_PORTRAIT_IMAGE_SIZE );
$this->assertIsString( $result );
$this->assertStringContainsString( '[web_stories_embed', $result );
$this->assertStringContainsString( $poster, $result );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace Google\Web_Stories\Tests\Integration\Admin;

use Google\Web_Stories\Story_Post_Type;
use Google\Web_Stories\Tests\Integration\DependencyInjectedTestCase;
use Google\Web_Stories\User\Preferences;

Expand Down Expand Up @@ -73,7 +74,7 @@ public function test_register(): void {
wp_set_current_user( $this->admin_id );
update_user_meta( $this->admin_id, Preferences::MEDIA_OPTIMIZATION_META_KEY, true );

$GLOBALS['current_screen'] = convert_to_screen( \Google\Web_Stories\Story_Post_Type::POST_TYPE_SLUG );
$GLOBALS['current_screen'] = convert_to_screen( Story_Post_Type::POST_TYPE_SLUG );

$this->instance->register();

Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/integration/tests/Admin/Customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace Google\Web_Stories\Tests\Integration\Admin;

use Google\Web_Stories\Admin\Customizer as TheCustomizer;
use Google\Web_Stories\Story_Post_Type;
use Google\Web_Stories\Tests\Integration\DependencyInjectedTestCase;
use WP_Customize_Manager;
use WP_Customize_Section;
Expand Down Expand Up @@ -316,7 +317,7 @@ public function test_render_stories(): void {

self::factory()->post->create(
[
'post_type' => \Google\Web_Stories\Story_Post_Type::POST_TYPE_SLUG,
'post_type' => Story_Post_Type::POST_TYPE_SLUG,
]
);

Expand Down
39 changes: 26 additions & 13 deletions tests/phpunit/integration/tests/Admin/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@

namespace Google\Web_Stories\Tests\Integration\Admin;

use Google\Web_Stories\Admin\Google_Fonts;
use Google\Web_Stories\Assets;
use Google\Web_Stories\Context;
use Google\Web_Stories\Decoder;
use Google\Web_Stories\Experiments;
use Google\Web_Stories\Font_Post_Type;
use Google\Web_Stories\Integrations\Site_Kit;
use Google\Web_Stories\Integrations\WooCommerce;
use Google\Web_Stories\Locale;
use Google\Web_Stories\Media\Types;
use Google\Web_Stories\Settings;
use Google\Web_Stories\Shopping\Shopping_Vendors;
use Google\Web_Stories\Story_Post_Type;
use Google\Web_Stories\Tests\Integration\Capabilities_Setup;
use Google\Web_Stories\Tests\Integration\DependencyInjectedTestCase;
use WP_UnitTest_Factory;
Expand Down Expand Up @@ -150,11 +163,11 @@ public function test_enqueue_assets_wrong_page(): void {
public function test_enqueue_assets(): void {
wp_set_current_user( self::$user_id );

$experiments = $this->createMock( \Google\Web_Stories\Experiments::class );
$experiments = $this->createMock( Experiments::class );
$experiments->method( 'get_experiment_statuses' )
->willReturn( [] );

$assets = $this->getMockBuilder( \Google\Web_Stories\Assets::class )->setMethods( [ 'get_asset_metadata' ] )->getMock();
$assets = $this->getMockBuilder( Assets::class )->setMethods( [ 'get_asset_metadata' ] )->getMock();
$assets->method( 'get_asset_metadata' )
->willReturn(
[
Expand All @@ -166,17 +179,17 @@ public function test_enqueue_assets(): void {
]
);

$site_kit = $this->injector->make( \Google\Web_Stories\Integrations\Site_Kit::class );
$decoder = $this->injector->make( \Google\Web_Stories\Decoder::class );
$locale = $this->injector->make( \Google\Web_Stories\Locale::class );
$google_fonts = $this->injector->make( \Google\Web_Stories\Admin\Google_Fonts::class );
$font_post_type = $this->injector->make( \Google\Web_Stories\Font_Post_Type::class );
$post_type = $this->injector->make( \Google\Web_Stories\Story_Post_Type::class );
$context = $this->injector->make( \Google\Web_Stories\Context::class );
$types = $this->injector->make( \Google\Web_Stories\Media\Types::class );
$shopping_vendors = $this->injector->make( \Google\Web_Stories\Shopping\Shopping_Vendors::class );
$woocommerce = $this->injector->make( \Google\Web_Stories\Integrations\WooCommerce::class );
$settings = $this->injector->make( \Google\Web_Stories\Settings::class );
$site_kit = $this->injector->make( Site_Kit::class );
$decoder = $this->injector->make( Decoder::class );
$locale = $this->injector->make( Locale::class );
$google_fonts = $this->injector->make( Google_Fonts::class );
$font_post_type = $this->injector->make( Font_Post_Type::class );
$post_type = $this->injector->make( Story_Post_Type::class );
$context = $this->injector->make( Context::class );
$types = $this->injector->make( Types::class );
$shopping_vendors = $this->injector->make( Shopping_Vendors::class );
$woocommerce = $this->injector->make( WooCommerce::class );
$settings = $this->injector->make( Settings::class );

$this->instance = new \Google\Web_Stories\Admin\Dashboard(
$experiments,
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/integration/tests/Admin/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace Google\Web_Stories\Tests\Integration\Admin;

use Google\Web_Stories\Admin\Google_Fonts;
use Google\Web_Stories\Admin\Meta_Boxes;
use Google\Web_Stories\Assets;
use Google\Web_Stories\Context;
use Google\Web_Stories\Decoder;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function set_up(): void {
parent::set_up();

$this->experiments = $this->createMock( Experiments::class );
$meta_boxes = $this->injector->make( \Google\Web_Stories\Admin\Meta_Boxes::class );
$meta_boxes = $this->injector->make( Meta_Boxes::class );
$decoder = $this->injector->make( Decoder::class );
$locale = $this->injector->make( Locale::class );
$google_fonts = $this->injector->make( Google_Fonts::class );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/integration/tests/Admin/Meta_Boxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function set_up(): void {
// is run multiple times, causing the story not to be available anymore.
$this->story_id = self::factory()->post->create(
[
'post_type' => \Google\Web_Stories\Story_Post_Type::POST_TYPE_SLUG,
'post_type' => Story_Post_Type::POST_TYPE_SLUG,
'post_title' => 'Meta Boxes Test Story',
'post_status' => 'publish',
'post_content' => 'Example content',
Expand Down

0 comments on commit 704525e

Please sign in to comment.