From b0d10199b79d8a9f07bcc22cbba0d391b48c59f5 Mon Sep 17 00:00:00 2001
From: Cameron <e107inc@gmail.com>
Date: Thu, 11 Feb 2021 08:51:02 -0800
Subject: [PATCH] Added getScope() to theme handler. Added more theme handler
 tests. Fixed duplicate entry in _blank/theme.xml

---
 e107_handlers/e107_class.php          |  21 +++
 e107_handlers/theme_handler.php       |  71 ++++++++-
 e107_tests/tests/unit/e107Test.php    |  33 ++++-
 e107_tests/tests/unit/e_themeTest.php | 200 +++++++++++++++++++++++++-
 e107_themes/_blank/theme.xml          |   4 -
 5 files changed, 315 insertions(+), 14 deletions(-)

diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php
index d5be4375c..63d79f789 100644
--- a/e107_handlers/e107_class.php
+++ b/e107_handlers/e107_class.php
@@ -2353,6 +2353,27 @@ class e107
 				return $libraryHandler->info($library);
 				break;
 
+			case 'files':
+				$info = $libraryHandler->info($library);
+				$ret = [];
+				if(!empty($info['files']['css']))
+				{
+					foreach($info['files']['css'] as $path => $other)
+					{
+						$ret['css'][] = $info['library_path'].'/'.$info['path'].'/'.$path;
+					}
+				}
+				if(!empty($info['files']['js']))
+				{
+					foreach($info['files']['js'] as $path => $other)
+					{
+						$ret['js'][] = $info['library_path'].'/'.$info['path'].'/'.$path;
+					}
+				}
+
+				return $ret;
+				break;
+
 			case 'preload':
 
 				$info = $libraryHandler->info($library);
diff --git a/e107_handlers/theme_handler.php b/e107_handlers/theme_handler.php
index e5a5a7d0d..c3316e57c 100644
--- a/e107_handlers/theme_handler.php
+++ b/e107_handlers/theme_handler.php
@@ -154,6 +154,71 @@ class e_theme
 		//	$ns->tablerender(TPVLAN_2, $text);
 	}
 
+	/**
+	 * @param string $type library | stylesheet (as defined in theme.xml)
+	 * @param string $scope  front | admin | all | auto (as defined in theme.xml)
+	 */
+	public function getScope($type, $scope)
+	{
+		if($scope === 'auto')
+		{
+			$scope = 'front';
+
+			if(deftrue('e_ADMIN_AREA', false))
+			{
+				$scope = 'admin';
+			}
+		}
+
+
+		if($type === 'library')
+		{
+			$themeXMLData = $this->get('library');
+		}
+		if($type === 'stylesheet')
+		{
+			$themeXMLData = $this->get('css');
+		}
+
+		$ret = [];
+
+		foreach($themeXMLData as $info)
+		{
+			$tmp = explode(',', $info['scope']);
+			foreach($tmp as $scp)
+			{
+				$scp = trim($scp);
+
+				if($scp === $scope || $scp === 'all')
+				{
+					$name = $info['name'];
+					unset($info['name']);
+					unset($info['scope']);
+					$ret[$name] = $info;
+				//	$ret[$name] = e107::library('files', $name);
+				}
+
+			}
+		}
+
+		return $ret;
+
+		/**
+		 * 					if($name === 'bootstrap' && !empty($info['version']) && (intval($info['version']) > 3))
+					{
+						$name .= (string) $info['version'];
+					}
+					elseif($name === 'fontawesome' && !empty($info['version']) && (intval($info['version']) > 4))
+					{
+						$name .= (string) $info['version'];
+					}
+		 */
+
+	}
+
+
+
+
 
 	/**
 	 * Load library dependencies.
@@ -180,6 +245,7 @@ class e_theme
 			return;
 		}
 
+		$loaded = [];
 
 		foreach($libraries as $library)
 		{
@@ -223,6 +289,7 @@ class e_theme
 				e107::library('load', $library['name']);
 				e107::library('preload', $library['name']);
 
+				$loaded[] = $library['name'];
 
 				continue;
 			}
@@ -231,10 +298,12 @@ class e_theme
 			{
 				e107::library('load', $library['name']);
 				e107::library('preload', $library['name']);
-
+				$loaded[] = $library['name'];
 				continue;
 			}
 		}
+
+		return $loaded;
 	}
 
 	/**
diff --git a/e107_tests/tests/unit/e107Test.php b/e107_tests/tests/unit/e107Test.php
index fed33cb38..148f3458e 100644
--- a/e107_tests/tests/unit/e107Test.php
+++ b/e107_tests/tests/unit/e107Test.php
@@ -729,13 +729,38 @@ class e107Test extends \Codeception\Test\Unit
 		$res = null;
 		$this->assertTrue($res);
 	}
-
+*/
 	public function testLibrary()
 	{
-		$res = null;
-		$this->assertTrue($res);
-	}
+		$e107 = $this->e107;
 
+		$expected = array (
+		  'css' =>
+		  array (
+		    0 => '{e_WEB}lib/font-awesome/5/css/all.min.css',
+		    1 => '{e_WEB}lib/font-awesome/5/css/v4-shims.min.css',
+		  ),
+		);
+
+		$result = $e107::library('files', 'fontawesome5');
+		$this->assertSame($expected, $result);
+
+		$expected = array (
+		  'css' =>
+		  array (
+		    0 => '{e_WEB}lib/bootstrap/5/css/bootstrap.min.css',
+		  ),
+		  'js' =>
+		  array (
+		    0 => '{e_WEB}lib/bootstrap/5/js/bootstrap.bundle.min.js',
+		  ),
+		);
+
+		$result = $e107::library('files', 'bootstrap5');
+		$this->assertSame($expected, $result);
+
+	}
+/*
 	public function testGetJs()
 	{
 		$res = null;
diff --git a/e107_tests/tests/unit/e_themeTest.php b/e107_tests/tests/unit/e_themeTest.php
index 83b8bd565..56095ee49 100644
--- a/e107_tests/tests/unit/e_themeTest.php
+++ b/e107_tests/tests/unit/e_themeTest.php
@@ -44,12 +44,202 @@
 		{
 
 		}
-
-		public function testLoadLibrary()
+*/
+		public function testGetScope()
 		{
+			$tests = array(
+				0   => array(
+					'theme'    => 'front',
+					'type'     => 'library',
+					'scope'    => 'front',
+					'expected' => array (
+						  'bootstrap' =>
+						  array (
+						    'version' => '3',
+						  ),
+						  'fontawesome' =>
+						  array (
+						    'version' => '5',
+						  ),
+						)
+					),
+				1   => array(
+					'theme'    => 'front',
+					'type'     => 'library',
+					'scope'    => 'all',
+					'expected' => array (
+						  'bootstrap' =>
+						  array (
+						    'version' => '3',
+						  ),
+						  'fontawesome' =>
+						  array (
+						    'version' => '5',
+						  ),
+						)
+					),
+				2   => array(
+					'theme'    => 'front',
+					'type'     => 'library',
+					'scope'    => 'admin',
+					'expected' => array (
+						  'bootstrap' =>
+						  array (
+						    'version' => '3',
+						  ),
+						  'fontawesome' =>
+						  array (
+						    'version' => '5',
+						  ),
+						  'bootstrap.editable' =>
+						  array (
+						    'version' => '',
+						  ),
+						)
+					),
+			   	3   => array(
+					'theme'    => '_blank',
+					'type'     => 'library',
+					'scope'    => 'front',
+					'expected' => array (
+						  'bootstrap' =>
+						  array (
+						    'version' => '3',
+						  ),
+						  'fontawesome' =>
+						  array (
+						    'version' => '4',
+						  ),
+						)
+					),
+
+			);
+
+			foreach($tests as $index => $var)
+			{
+				$result = e107::getTheme($var['theme'])->getScope($var['type'], $var['scope']);
+				$this->assertSame($var['expected'], $result, 'Test #'.$index.' failed.');
+			}
 
 		}
 
+
+		public function testLoadLibrary()
+		{
+			$tests = array(
+				0   => array(
+					'theme'    => 'front',
+					'scope'    => 'front',
+					'expected' => ['bootstrap', 'fontawesome5']
+					),
+				1   => array(
+					'theme'    => 'front',
+					'scope'    => 'admin',
+					'expected' => ['bootstrap', 'fontawesome5', 'bootstrap.editable']
+					),
+			   	2   => array(
+					'theme'    => '_blank',
+					'scope'    => 'front',
+					'expected' => ['bootstrap', 'fontawesome']
+					),
+
+			);
+
+			foreach($tests as $index => $var)
+			{
+				$loaded = e107::getTheme($var['theme'],true)->loadLibrary($var['scope']);
+				$this->assertSame($var['expected'], $loaded, 'Test #'.$index.' failed.');
+			}
+
+		//	var_export($loaded);
+
+
+
+
+		}
+
+		public function testGet()
+		{
+			$tests = array(
+				0   => array(
+					'theme'    => 'front',
+					'type'     => 'library',
+					'expected' => array (
+						  0 =>
+						  array (
+						    'name' => 'bootstrap',
+						    'version' => '3',
+						    'scope' => 'all',
+						  ),
+						  1 =>
+						  array (
+						    'name' => 'fontawesome',
+						    'version' => '5',
+						    'scope' => 'all',
+						  ),
+						  2 =>
+						  array (
+						    'name' => 'bootstrap.editable',
+						    'version' => '',
+						    'scope' => 'admin',
+						  ),
+						)
+					),
+				1   => array(
+					'theme'    => 'bootstrap3',
+					'type'     => 'library',
+					'expected' => array (
+						  0 =>
+						  array (
+						    'name' => 'bootstrap',
+						    'version' => '3',
+						    'scope' => 'all',
+						  ),
+						  1 =>
+						  array (
+						    'name' => 'fontawesome',
+						    'version' => '5',
+						    'scope' => 'all',
+						  ),
+						  2 =>
+						  array (
+						    'name' => 'bootstrap.editable',
+						    'version' => '',
+						    'scope' => 'admin',
+						  ),
+						)
+					),
+			   	2   => array(
+					'theme'    => '_blank',
+					'type'     => 'library',
+					'expected' => array (
+						  0 =>
+						  array (
+						    'name' => 'bootstrap',
+						    'version' => '3',
+						    'scope' => 'all',
+						  ),
+						  1 =>
+						  array (
+						    'name' => 'fontawesome',
+						    'version' => '4',
+						    'scope' => 'all',
+						  ),
+						)
+					),
+
+			);
+
+
+			foreach($tests as $index => $var)
+			{
+				$result = e107::getTheme($var['theme'])->get($var['type']);
+				$this->assertSame($var['expected'], $result, 'Test #'.$index.' failed');
+			}
+
+
+		}
+/*
 		public function testParse_theme_php()
 		{
 
@@ -168,14 +358,14 @@
 
 
 
-		public function testLoadLayout()
-		{
+	//	public function testLoadLayout()
+	//	{
 			// $res = e_theme::loadLayout('full', 'bootstrap4');
 
 		//	var_dump($res);
 
 
-		}
+	//	}
 /*
 		public function testGetThemesMigrations()
 		{
diff --git a/e107_themes/_blank/theme.xml b/e107_themes/_blank/theme.xml
index d3c3a04a2..47f6f5808 100644
--- a/e107_themes/_blank/theme.xml
+++ b/e107_themes/_blank/theme.xml
@@ -20,10 +20,6 @@
 		<word>empty</word>
 		<word>template</word>
 	</keywords>
-	<libraries>
-		<library name="bootstrap" version="3" scope="all"/>
-		<library name="fontawesome" scope="all"/>
-	</libraries>
 	<layouts>
 		<layout name='default' title='Default' default='true' />
 		<layout name='home' title='Home'>