Skip to content

Commit

Permalink
backport phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadam committed Apr 30, 2024
1 parent 2f6388f commit 1e1d4b4
Show file tree
Hide file tree
Showing 9 changed files with 633 additions and 107 deletions.
3 changes: 0 additions & 3 deletions php/tests/AdodbTest.php
@@ -1,8 +1,5 @@
<?php

error_reporting(E_ALL | E_STRICT);
set_include_path(realpath(__DIR__).'/../extlib/adodb5');

use PHPUnit\Framework\TestCase;

class AdodbTest extends TestCase {
Expand Down
2 changes: 1 addition & 1 deletion php/tests/ArchiveLibTest.php
@@ -1,7 +1,7 @@
<?php

use PHPUnit\Framework\TestCase;
include_once('php/lib/archive_lib.php');
require_once('archive_lib.php');

class ArchiveLibTest extends TestCase {

Expand Down
138 changes: 63 additions & 75 deletions php/tests/BaseObjectTest.php
Expand Up @@ -4,81 +4,69 @@

class BaseObjectTest extends TestCase {
private static $_cache_driver;
public function testIssetWithOverloading() {

include_once("php/mt.php");
include_once("php/lib/MTUtil.php");

$cfg_file = realpath( "t/mysql-test.cfg" );
$mt = MT::get_instance(1, $cfg_file);
$ctx =& $mt->context();

// Test some objects inheriting ObjectBase class.

require_once( "php/lib/class.mt_config.php" );
$config = new Config;
$config->Load();
$this->assertTrue( isset( $config->id ) );

require_once( "php/lib/class.mt_author.php" );
$author = new Author;
$author->Load();
$this->assertTrue( isset( $author->id ) );

require_once( "php/lib/class.mt_entry.php" );
$entry = new Entry;
$entry->id = 1;
$this->assertTrue( isset( $entry->id ) );
$this->assertEquals( 'entry_', $entry->_prefix );

// protected variable call (bugid:113105, MTC-9543)
$this->assertNull( $entry->_has_meta );
$this->assertFalse( isset( $entry->_has_meta ) );

// ignore E_DEPRECATED for some tests
error_reporting(error_reporting() & ~E_DEPRECATED);

// dynamic properties still works (__set/__get/__isset magic methods)
$entry->unknown = 'val';
$this->assertTrue( isset( $entry->unknown ) );
$this->assertEquals( 'val', $entry->unknown );

// meta field in the form of order_by MT tag attributes work
$meta_field1 = 'field:my_field1';
$meta_field2 = 'field:my_field2';
$entry->$meta_field1 = 'my_field1_val';
$this->assertTrue( isset( $entry->$meta_field1 ) );
$this->assertEquals( 'my_field1_val', $entry->$meta_field1 );
$this->assertFalse( isset( $entry->$meta_field2 ) );
$this->assertNull( $entry->$meta_field2 );

error_reporting(error_reporting() | E_DEPRECATED);

// fixed Dynamic publishing error occurred with memcached environment. bugid: 113546
$mt->config('MemcachedServers', '127.0.0.1:11211');
$obj_names = array(
'asset' => 'Asset',
'author' => 'Author',
'blog' => 'Blog',
'category' => 'Category',
'comment' => 'Comment',
'entry' => 'Entry',
'folder' => 'Folder',
'page' => 'Page',
'tbping' => 'TBPing',
'template' => 'Template',
'website' => 'Website');
foreach ($obj_names as $table => $name) {
require_once("php/lib/class.mt_$table.php");
$obj = new $name;
$obj->Load();

$this->cache("$table:".$obj->id, $obj);
$obj_cache = $this->load_cache("$table:".$obj->id);
$this->assertInstanceOf("$name", $obj_cache);
}
public function testIssetWithOverloading() {

// Test some objects inheriting ObjectBase class.

require_once( "php/lib/class.mt_config.php" );
$config = new Config;
$config->Load();
$this->assertTrue( isset( $config->id ) );

require_once( "php/lib/class.mt_author.php" );
$author = new Author;
$author->Load();
$this->assertTrue( isset( $author->id ) );

require_once( "php/lib/class.mt_entry.php" );
$entry = new Entry;
$entry->id = 1;
$this->assertTrue( isset( $entry->id ) );
$this->assertEquals( 'entry_', $entry->_prefix );

// protected variable call (bugid:113105, MTC-9543)
$this->assertNull( $entry->_has_meta );
$this->assertFalse( isset( $entry->_has_meta ) );

// dynamic properties still works (__set/__get/__isset magic methods)
$entry->unknown = 'val';
$this->assertTrue( isset( $entry->unknown ) );
$this->assertEquals( 'val', $entry->unknown );

// meta field in the form of order_by MT tag attributes work
$meta_field1 = 'field:my_field1';
$meta_field2 = 'field:my_field2';
$entry->$meta_field1 = 'my_field1_val';
$this->assertTrue( isset( $entry->$meta_field1 ) );
$this->assertEquals( 'my_field1_val', $entry->$meta_field1 );
$this->assertFalse( isset( $entry->$meta_field2 ) );
$this->assertNull( $entry->$meta_field2 );

// fixed Dynamic publishing error occurred with memcached environment. bugid: 113546
MT::get_instance()->config('MemcachedServers', '127.0.0.1:11211');
$obj_names = array(
'asset' => 'Asset',
'author' => 'Author',
'blog' => 'Blog',
'category' => 'Category',
'comment' => 'Comment',
'entry' => 'Entry',
'folder' => 'Folder',
'page' => 'Page',
'tbping' => 'TBPing',
'template' => 'Template',
'website' => 'Website');
foreach ($obj_names as $table => $name) {
require_once("class.mt_$table.php");
$obj = new $name;
$obj->Load();

$this->cache("$table:".$obj->id, $obj);
$obj_cache = $this->load_cache("$table:".$obj->id);
$this->assertInstanceOf("$name", $obj_cache);
}

}
}

// Objcet cache
private function cache($key, $obj) {
Expand All @@ -99,7 +87,7 @@ private function load_cache($key) {

private function cache_driver() {
if (empty(self::$_cache_driver)) {
require_once("class.basecache.php");
require_once('class.basecache.php');
try {
self::$_cache_driver = CacheProviderFactory::get_provider('memcached');
} catch (Exception $e) {
Expand Down
133 changes: 133 additions & 0 deletions php/tests/ClassTest.php
@@ -0,0 +1,133 @@
<?php

use PHPUnit\Framework\TestCase;

require_once('Mockdata.php');

class ClassTest extends TestCase {

public function testEntry() {

$category = Mockdata::makeCategory();
$entry = Mockdata::makeEntry();

$entry2 = new Entry();
$entry2->Load($entry->id);
$this->assertEquals('Entry', get_class($entry2));
$this->assertEquals($entry->id, $entry2->id);

$template = $entry2->template();
$this->assertEquals('Template', get_class($template));
$this->assertEquals('1', $template->id);

$comment = Mockdata::makeComment();
$comments = $entry->comments();
$this->assertEquals('Comment', get_class($comments[0]));
$this->assertEquals($comment->id, $comments[0]->id);

$trackback = MockData::makeTrackback();

$trackback2 = $entry->trackback();
$this->assertEquals('Trackback', get_class($trackback2));
$this->assertEquals($trackback->id, $trackback2->id);

$ping = MockData::makeTbping(['entry_id' => $entry->id, 'trackback_id' => $trackback->id]);
$pings = $entry->pings();
$this->assertEquals('TBPing', get_class($pings[0]));
$this->assertEquals($ping->id, $pings[0]->id);

$trackback2 = $ping->trackback();
$this->assertEquals('Trackback', get_class($trackback2));
$this->assertEquals($trackback2->id, $trackback->id);

$category2 = $trackback2->category();
$this->assertEquals('Category', get_class($category2));
$this->assertEquals($category->id, $category2->id);
}

public function testObjectAsset() {

$entry = MockData::makeEntry();
$asset = MockData::makeAsset();
$oasset = MockData::makeObjectAsset(['object_ds' => 'entry']);

$oasset2 = new ObjectAsset();
$oasset2->Load($oasset->id);
$this->assertEquals('ObjectAsset', get_class($oasset2));
$this->assertEquals($oasset->id, $oasset2->id);

$asset2 = $oasset2->asset();
$this->assertEquals('Asset', get_class($asset2));
$this->assertEquals($asset->id, $asset2->id);

$entry2 = $oasset2->related_object();
$this->assertEquals('Entry', get_class($entry2));
$this->assertEquals($entry->id, $entry2->id);
}

public function testObjectScore() {

$entry = MockData::makeEntry();
$oscore = MockData::makeObjectScore(['object_ds' => 'entry']);
$oscore2 = new ObjectScore();
$oscore2->Load($oscore->id);
$this->assertEquals('ObjectScore', get_class($oscore2));
$this->assertEquals($oscore->id, $oscore2->id);

$entry2 = $oscore2->related_object();
$this->assertEquals('Entry', get_class($entry2));
$this->assertEquals($entry->id, $entry2->id);
}

public function testObjectTag() {

$entry = Mockdata::makeEntry(['status' => 2]);
$otag = MockData::makeObjectTag(['object_datasource' => 'entry', 'tag_id' => 1]);

$otag2 = new ObjectTag();
$otag2->Load($otag->id);
$this->assertEquals('ObjectTag', get_class($otag2));
$this->assertEquals($otag->id, $otag2->id);

$entry2 = $otag2->related_object();
$this->assertEquals('Entry', get_class($entry2));
$this->assertEquals($entry->id, $entry2->id);

$tag = $otag2->tag();
$this->assertEquals('Tag', get_class($tag));
}

public function testPage() {

$page = MockData::makePage();

require_once('class.mt_folder.php');
$folder = new Folder();
$folder->blog_id = 1;
$folder->class = 'folder';
$folder->category_category_set_id = 0;
$folder->label = '';
$folder->save();

require_once('class.mt_placement.php');
$placement = new Placement();
$placement->blog_id = 1;
$placement->entry_id = $page->id;
$placement->is_primary = 1;
$placement->category_id = $folder->id;
$placement->save();

$folder2 = $page->folder();
$this->assertEquals('Folder', get_class($folder2));
$this->assertEquals($folder2->id, $folder->id);
}

public function testTemplate() {

$template = MockData::makeTemplate(['type' => 'index', 'name' => 'mytemplate']);

$blog = $template->blog();
$this->assertEquals('Website', get_class($blog));
$this->assertEquals(1, $blog->id);
}
}
3 changes: 3 additions & 0 deletions php/tests/InitTestDB.php
Expand Up @@ -2,3 +2,6 @@

system('MT_CONFIG=mysql-test.cfg perl -It/lib -Ilib -Iextlib -MMT::Test=:db -E "say \"Initialized test DB.\""');

set_include_path(realpath(__DIR__). '/../');
require_once('mt.php');
MT::get_instance(1, realpath( "t/mysql-test.cfg" ));
2 changes: 1 addition & 1 deletion php/tests/MTSerializeTest.php
Expand Up @@ -4,7 +4,7 @@

class MTSerializeTest extends TestCase {
public function testGetInstace() {
include_once('php/lib/MTSerialize.php');
require_once('MTSerialize.php');
$serializer1 = MTSerialize::get_instance();
$this->assertInstanceOf('MTSerialize', $serializer1);

Expand Down
16 changes: 8 additions & 8 deletions php/tests/MemcachedTest.php
Expand Up @@ -2,14 +2,14 @@

use PHPUnit\Framework\TestCase;

include_once("php/lib/mtcache_base.php");
include_once("php/lib/class.basecache.php");
include_once("php/mt.php");
require_once('mtcache_base.php');
require_once('class.basecache.php');
require_once('mt.php');

class MemcachedTest extends TestCase {

public function testMain() {
$mt = MT::get_instance(1, realpath( "t/mysql-test.cfg" ));
$mt = MT::get_instance();
$this->_testCacheLib($mt);
$this->_testMTCacheSession($mt);
$this->_testCacheSession($mt);
Expand All @@ -25,26 +25,26 @@ public function _testCacheLib($mt) {
}

public function _testMTCacheSession($mt) {
include_once("php/lib/mtcache_session.php");
require_once('mtcache_session.php');
$a = new MTCache_session();
$this->assertCache($a, true);
}

public function _testCacheSession($mt) {
include_once("php/lib/class.cachesession.php");
require_once('class.cachesession.php');
$a = new CacheSession();
$this->assertCache($a, true);
}

public function _testCacheMemcache($mt) {
include_once("php/lib/class.cachememcached.php");
require_once('class.cachememcached.php');
$mt->config('MemcachedServers', '127.0.0.1:11211');
$a = new CacheMemcached();
$this->assertCache($a);
}

public function _testMTCacheMemcached($mt) {
include_once("php/lib/mtcache_memcached.php");
require_once('mtcache_memcached.php');
$a = new MTCache_memcached();
$a->connect('127.0.0.1:11211');
$this->assertCache($a);
Expand Down

0 comments on commit 1e1d4b4

Please sign in to comment.