Skip to content

Commit bfbb949

Browse files
committed
Add migration unit test.
1 parent 6ae4807 commit bfbb949

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

migrations/handle_subscriptions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
namespace phpbb\webpushnotifications\migrations;
1212

13-
use phpbb\db\migration\container_aware_migration;
13+
use phpbb\db\migration\migration;
1414

15-
class handle_subscriptions extends container_aware_migration
15+
class handle_subscriptions extends migration
1616
{
1717
public function effectively_installed()
1818
{
@@ -24,7 +24,7 @@ public function revert_data(): array
2424
return [
2525
['custom', [[$this, 'update_subscriptions']]],
2626
['if', [
27-
($this->container->has('notification.method.webpush')),
27+
($this->db_tools->sql_table_exists($this->table_prefix . 'notification_push')),
2828
['custom', [[$this, 'copy_subscription_tables']]],
2929
]],
3030
['if', [
@@ -47,7 +47,7 @@ public function update_subscriptions()
4747
$user_notifications_table = $this->table_prefix . 'user_notifications';
4848

4949
// Check if webpush notification method exists in phpBB core (as of phpBB 4.0)
50-
$core_webpush_exists = $this->container->has('notification.method.webpush');
50+
$core_webpush_exists = $this->db_tools->sql_table_exists($this->table_prefix . 'notification_push');
5151

5252
/*
5353
* If webpush notification method exists in phpBB core,

tests/notification/fixtures/webpush_notification.type.post.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,9 @@
283283
<value>1</value>
284284
</row>
285285
</table>
286+
<table name="phpbb_config">
287+
<column>config_name</column>
288+
<column>config_value</column>
289+
<column>is_dynamic</column>
290+
</table>
286291
</dataset>

tests/notification/notification_method_webpush_test.php

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ protected function setUp(): void
9191

9292
global $db, $config, $user, $auth, $cache, $phpbb_container, $phpbb_dispatcher;
9393

94-
// $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper')
95-
// ->disableOriginalConstructor()
96-
// ->getMock();
9794
$db = $this->db = $this->new_dbal();
9895
$config = $this->config = new \phpbb\config\config([
9996
'allow_privmsg' => true,
@@ -438,4 +435,97 @@ protected function get_messages_for_subscription($client_hash): array
438435

439436
return $response_data['data']['messages'];
440437
}
438+
439+
/**
440+
* @depends test_get_subscription
441+
*/
442+
public function test_export_data_with_migration(): void
443+
{
444+
global $phpbb_root_path, $phpEx;
445+
446+
require_once __DIR__ . '/../../migrations/handle_subscriptions.php'; // load the extension migration
447+
448+
$this->config['wpn_webpush_enable'] = '1';
449+
$this->config['webpush_enable'] = '';
450+
$this->config['webpush_vapid_public'] = '';
451+
$this->config['webpush_vapid_private'] = '';
452+
453+
$config_db = new \phpbb\config\db($this->container->get('dbal.conn'), $this->container->get('cache.driver'), 'phpbb_config');
454+
foreach ($this->config as $config_name => $config_value)
455+
{
456+
$config_db->set($config_name, $config_value);
457+
}
458+
459+
$factory = new \phpbb\db\tools\factory();
460+
$db_tools = $factory->get($this->db);
461+
462+
$phpbb_notification_push_data = [
463+
'COLUMNS' => [
464+
'notification_type_id' => ['USINT', 0],
465+
'item_id' => ['ULINT', 0],
466+
'item_parent_id' => ['ULINT', 0],
467+
'user_id' => ['ULINT', 0],
468+
'push_data' => ['MTEXT', ''],
469+
'notification_time' => ['TIMESTAMP', 0]
470+
],
471+
'PRIMARY_KEY' => ['notification_type_id', 'item_id', 'item_parent_id', 'user_id'],
472+
];
473+
$db_tools->sql_create_table('phpbb_notification_push', $phpbb_notification_push_data);
474+
475+
$phpbb_push_subscriptions_data = [
476+
'COLUMNS' => [
477+
'subscription_id' => ['ULINT', null, 'auto_increment'],
478+
'user_id' => ['ULINT', 0],
479+
'endpoint' => ['TEXT', ''],
480+
'expiration_time' => ['TIMESTAMP', 0],
481+
'p256dh' => ['VCHAR', ''],
482+
'auth' => ['VCHAR', ''],
483+
],
484+
'PRIMARY_KEY' => ['subscription_id', 'user_id'],
485+
];
486+
$db_tools->sql_create_table('phpbb_push_subscriptions', $phpbb_push_subscriptions_data);
487+
488+
$config_tool = new \phpbb\db\migration\tool\config($config_db);
489+
$this->container->set('migrator.tool.config', $config_tool);
490+
$tools_collection = new \phpbb\di\service_collection($this->container);
491+
$tools_collection->add('migrator.tool.config');
492+
493+
$migrator = new \phpbb\db\migrator(
494+
$this->container,
495+
$config_db,
496+
$this->db,
497+
$db_tools,
498+
'phpbb_migrations',
499+
$phpbb_root_path,
500+
$phpEx,
501+
'phpbb_',
502+
$tools_collection,
503+
new \phpbb\db\migration\helper()
504+
);
505+
$migrator->create_migrations_table();
506+
507+
$migration_class = '\phpbb\webpushnotifications\migrations\handle_subscriptions';
508+
$migrator->populate_migrations([$migration_class]);
509+
510+
while ($migrator->migration_state($migration_class) !== false)
511+
{
512+
$migrator->revert($migration_class);
513+
}
514+
515+
$sql = 'SELECT config_name, config_value FROM ' . CONFIG_TABLE . "
516+
WHERE config_name IN('webpush_enable', 'webpush_vapid_public', 'webpush_vapid_private')";
517+
$result = $this->db->sql_query($sql);
518+
$data = $this->db->sql_fetchrowset($result);
519+
$this->db->sql_freeresult($result);
520+
521+
$exported_config_data = [];
522+
foreach ($data as $row)
523+
{
524+
$exported_config_data[$row['config_name']] = $row['config_value'];
525+
}
526+
527+
$this->assertEquals($this->config['wpn_webpush_enable'], $exported_config_data['webpush_enable']);
528+
$this->assertEquals($this->config['wpn_webpush_vapid_public'], $exported_config_data['webpush_vapid_public']);
529+
$this->assertEquals($this->config['wpn_webpush_vapid_private'], $exported_config_data['webpush_vapid_private']);
530+
}
441531
}

0 commit comments

Comments
 (0)