PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/static/testsuite/Legacy/ObsoleteCodeTest.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 317 lines | 165 code | 23 blank | 129 comment | 6 complexity | b727f4693c5b6e68cb1c64485f0bd3c6 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category tests
  22. * @package static
  23. * @subpackage Legacy
  24. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  25. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  26. */
  27. /**
  28. * Tests to find various obsolete code usage
  29. * (deprecated and removed Magento 1 legacy methods, properties, classes, etc.)
  30. */
  31. class Legacy_ObsoleteCodeTest extends PHPUnit_Framework_TestCase
  32. {
  33. /**
  34. * Message text that is used to render suggestions
  35. */
  36. const SUGGESTION_MESSAGE = 'Use "%s" instead.';
  37. /**
  38. * In-memory cache for the configuration files
  39. *
  40. * @var array
  41. */
  42. protected static $_configFilesCache = array();
  43. /**
  44. * @param string $file
  45. * @dataProvider phpFileDataProvider
  46. */
  47. public function testPhpFile($file)
  48. {
  49. $content = file_get_contents($file);
  50. $this->_testObsoleteClasses($content, $file);
  51. $this->_testObsoleteMethods($content, $file);
  52. $this->_testObsoleteMethodArguments($content);
  53. $this->_testObsoleteProperties($content, $file);
  54. $this->_testObsoleteActions($content, $file);
  55. $this->_testObsoleteConstants($content, $file);
  56. $this->_testObsoletePropertySkipCalculate($content);
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function phpFileDataProvider()
  62. {
  63. return Utility_Files::init()->getPhpFiles();
  64. }
  65. /**
  66. * @param string $file
  67. * @dataProvider xmlFileDataProvider
  68. */
  69. public function testXmlFile($file)
  70. {
  71. $content = file_get_contents($file);
  72. $this->_testObsoleteClasses($content, $file);
  73. }
  74. /**
  75. * @return array
  76. */
  77. public function xmlFileDataProvider()
  78. {
  79. return Utility_Files::init()->getXmlFiles();
  80. }
  81. /**
  82. * @param string $file
  83. * @dataProvider jsFileDataProvider
  84. */
  85. public function testJsFile($file)
  86. {
  87. $content = file_get_contents($file);
  88. $this->_testObsoletePropertySkipCalculate($content);
  89. }
  90. /**
  91. * @return array
  92. */
  93. public function jsFileDataProvider()
  94. {
  95. return Utility_Files::init()->getJsFiles();
  96. }
  97. /**
  98. * @param string $content
  99. * @param string $file
  100. */
  101. protected function _testObsoleteClasses($content, $file)
  102. {
  103. $declarations = $this->_getRelevantConfigEntities('obsolete_classes*.php', $content, $file);
  104. foreach ($declarations as $declaration) {
  105. list($entity, $suggestion) = $declaration;
  106. $this->_assertNotRegExp('/[^a-z\d_]' . preg_quote($entity, '/') . '[^a-z\d_]/iS', $content,
  107. "Class '$entity' is obsolete. $suggestion"
  108. );
  109. }
  110. }
  111. /**
  112. * @param string $content
  113. * @param string $file
  114. */
  115. protected function _testObsoleteMethods($content, $file)
  116. {
  117. $declarations = $this->_getRelevantConfigEntities('obsolete_methods*.php', $content, $file);
  118. foreach ($declarations as $declaration) {
  119. list($method, $suggestion) = $declaration;
  120. $this->_assertNotRegExp('/[^a-z\d_]' . preg_quote($method, '/') . '\s*\(/iS', $content,
  121. "Method '$method' is obsolete. $suggestion"
  122. );
  123. }
  124. }
  125. /**
  126. * @param string $content
  127. */
  128. protected function _testObsoleteMethodArguments($content)
  129. {
  130. $this->_assertNotRegExp('/[^a-z\d_]getTypeInstance\s*\(\s*[^\)]+/iS', $content,
  131. 'Backwards-incompatible change: method getTypeInstance() is not supposed to be invoked with any arguments.'
  132. );
  133. $this->_assertNotRegExp('/\->getUsedProductIds\(([^\)]+,\s*[^\)]+)?\)/', $content,
  134. 'Backwards-incompatible change: method getUsedProductIds($product)'
  135. . ' must be invoked with one and only one argument - product model object'
  136. );
  137. $this->_assertNotRegExp('#->_setActiveMenu\([\'"]([\w\d/_]+)[\'"]\)#Ui', $content,
  138. 'Backwards-incompatible change: method _setActiveMenu()'
  139. . ' must be invoked with menu item identifier than xpath for menu item'
  140. );
  141. $this->assertEquals(0,
  142. preg_match('#Mage::getSingleton\([\'"]Mage_Backend_Model_Auth_Session[\'"]\)'
  143. . '([\s]+)?->isAllowed\(#Ui', $content),
  144. 'Backwards-incompatible change: method isAllowed()'
  145. . ' must be invoked from Mage::getSingleton(\'Mage_Code_Model_Authorization\')->isAllowed($resource)'
  146. );
  147. $this->_assertNotRegExp(
  148. '#Mage::getSingleton\([\'"]Mage_Core_Model_Authorization[\'"]\)'
  149. . '([\s]+)?->isAllowed\([\'"]([\w\d/_]+)[\'"]\)#Ui',
  150. $content,
  151. 'Backwards-incompatible change: method isAllowed()'
  152. . ' must be invoked with acl item identifier than xpath for acl item');
  153. }
  154. /**
  155. * @param string $content
  156. * @param string $file
  157. */
  158. protected function _testObsoleteProperties($content, $file)
  159. {
  160. $declarations = $this->_getRelevantConfigEntities('obsolete_properties*.php', $content, $file);
  161. foreach ($declarations as $declaration) {
  162. list($entity, $suggestion) = $declaration;
  163. $this->_assertNotRegExp('/[^a-z\d_]' . preg_quote($entity, '/') . '[^a-z\d_]/iS', $content,
  164. "Property '$entity' is obsolete. $suggestion"
  165. );
  166. }
  167. }
  168. /**
  169. * @param string $content
  170. */
  171. protected function _testObsoleteActions($content)
  172. {
  173. $suggestion = 'Resizing images upon the client request is obsolete, use server-side resizing instead';
  174. $this->_assertNotRegExp('#[^a-z\d_/]catalog/product/image[^a-z\d_/]#iS', $content,
  175. "Action 'catalog/product/image' is obsolete. $suggestion"
  176. );
  177. }
  178. /**
  179. * @param string $content
  180. * @param string $file
  181. */
  182. protected function _testObsoleteConstants($content, $file)
  183. {
  184. $declarations = $this->_getRelevantConfigEntities('obsolete_constants*.php', $content, $file);
  185. foreach ($declarations as $declaration) {
  186. list($entity, $suggestion) = $declaration;
  187. $this->_assertNotRegExp('/[^a-z\d_]' . preg_quote($entity, '/') . '[^a-z\d_]/iS', $content,
  188. "Constant '$entity' is obsolete. $suggestion"
  189. );
  190. }
  191. }
  192. /**
  193. * @param string $content
  194. */
  195. protected function _testObsoletePropertySkipCalculate($content)
  196. {
  197. $this->_assertNotRegExp('/[^a-z\d_]skipCalculate[^a-z\d_]/iS', $content,
  198. "Configuration property 'skipCalculate' is obsolete."
  199. );
  200. }
  201. /**
  202. * Retrieve configuration items, whose 'class_scope' match to the content, in the following format:
  203. * array(
  204. * array('<entity>', '<suggestion>'),
  205. * ...
  206. * )
  207. *
  208. * @param string $fileNamePattern
  209. * @param string $content
  210. * @param string $file
  211. * @return array
  212. */
  213. protected function _getRelevantConfigEntities($fileNamePattern, $content, $file)
  214. {
  215. $result = array();
  216. foreach ($this->_loadConfigFiles($fileNamePattern) as $info) {
  217. $class = $info['class_scope'];
  218. $regexp = '/(class|extends)\s+' . preg_quote($class, '/') . '(\s|;)/S';
  219. /* Note: strpos is used just to prevent excessive preg_match calls */
  220. if ($class && (!strpos($content, $class) || !preg_match($regexp, $content))) {
  221. continue;
  222. }
  223. if ($info['directory']) {
  224. if (0 !== strpos(str_replace('\\', '/', $file), str_replace('\\', '/', $info['directory']))) {
  225. continue;
  226. }
  227. }
  228. $result[] = array($info['obsolete_entity'], $info['suggestion']);
  229. }
  230. return $result;
  231. }
  232. /**
  233. * Load configuration data from the files that match a glob-pattern
  234. *
  235. * @param string $fileNamePattern
  236. * @return array
  237. */
  238. protected function _loadConfigFiles($fileNamePattern)
  239. {
  240. if (isset(self::$_configFilesCache[$fileNamePattern])) {
  241. return self::$_configFilesCache[$fileNamePattern];
  242. }
  243. $config = array();
  244. foreach (glob(dirname(__FILE__) . '/_files/' . $fileNamePattern, GLOB_BRACE) as $configFile) {
  245. $config = array_merge($config, include($configFile));
  246. }
  247. self::$_configFilesCache[$fileNamePattern] = $config;
  248. return $config;
  249. }
  250. /**
  251. * Custom replacement for assertNotRegexp()
  252. *
  253. * In this particular test the original assertNotRegexp() cannot be used
  254. * because of too large text $content, which obfuscates tests output
  255. *
  256. * @param string $regex
  257. * @param string $content
  258. * @param string $message
  259. */
  260. protected function _assertNotRegexp($regex, $content, $message)
  261. {
  262. $this->assertSame(0, preg_match($regex, $content), $message);
  263. }
  264. /**
  265. * Add class rule
  266. *
  267. * @param string $name
  268. * @param null|string $suggestion
  269. * @param null|string $directory
  270. * @return array
  271. */
  272. protected function _getClassRule($name, $suggestion = null, $directory = null)
  273. {
  274. return $this->_getRule($name, null, $suggestion, $directory);
  275. }
  276. /**
  277. * Get rule
  278. *
  279. * @param string $obsoleteEntity
  280. * @param null|string $classScope
  281. * @param null|string $suggestion
  282. * @param null|string $directory
  283. * @return array
  284. */
  285. protected function _getRule($obsoleteEntity, $classScope = null, $suggestion = null, $directory = null)
  286. {
  287. return array(
  288. 'obsolete_entity' => $obsoleteEntity,
  289. 'class_scope' => $classScope,
  290. 'suggestion' => $suggestion,
  291. 'directory' => $directory
  292. );
  293. }
  294. }